Tim Wilkinson
Tim Wilkinson

Reputation: 3801

jQuery find non specific elements with certain attribute

Is there a way in jQuery to call non specific elements with a certain attribute? I know you can search for a div for example with a set attribute.

$('div[data-snap-ignore="true"]');

But I have various elements that have the attribute data-snap-ignore that are not always divs, they can be sections, uls, buttons, all sorts really. However the following doesn't work.

$('[data-snap-ignore="true"]');

Upvotes: 1

Views: 141

Answers (3)

patel.milanb
patel.milanb

Reputation: 5992

you could also use filter()

$('*').filter(function(){return $(this).data('myAttr') == true ;});

Or just: $('[data-myAttr]')

Or

$(':data').each(function(){ // your condition });;       // All elements with data  
$(':not(:data)').each(function{}()); // All elements without data

Upvotes: 0

raina77ow
raina77ow

Reputation: 106375

If you're looking for elements just having an attribute (without any specific value to target), drop the value part:

$('[data-snap-ignore]');

Otherwise (if you're targeting elements with data-snap-ignore set to true) it should work fine with the code given - no asterisk's required.

Upvotes: 0

Adil
Adil

Reputation: 148110

Try using * to include all types, although it might need that.

Live Demo

$('*[data-snap-ignore="true"]');

Upvotes: 4

Related Questions