rostamiani rostamiani - 1 year ago 71
jQuery Question

How to find all tags with a specific data- attribute?

I need to find all tags that has a 'error' data binded to them using Jquery.
This is what I done. It's not working because I added the data attribute by code.

$('form input').data('error',1);

console.log( $('form input[data-error]') ); // This is empty

How can I retrieve all of them?

Answer Source

Use filter()

$('form input').filter(function(){
     return $(this).data('error'); // adjust if boolean not sufficient    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download