rostamiani rostamiani - 4 months ago 10
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

Use filter()

$('form input').filter(function(){
     return $(this).data('error'); // adjust if boolean not sufficient    
}).doSomething();