How can I find all elements without a certain data-attribute?
I've tried:
$list.find('li:not([data-stuff])');
But it doesn't work.
How can I find all elements without a certain data-attribute?
I've tried:
$list.find('li:not([data-stuff])');
But it doesn't work.
Share Improve this question edited Dec 24, 2015 at 18:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 11, 2013 at 15:58 VicVic 8,9917 gold badges47 silver badges56 bronze badges 3-
what is
$list
element, can you share the html as well – Arun P Johny Commented Nov 11, 2013 at 15:59 - is the data attribute modified by script or is it present in the html markup – Arun P Johny Commented Nov 11, 2013 at 16:01
- data is added by script – Vic Commented Nov 11, 2013 at 16:01
2 Answers
Reset to default 6jQuery stores data
attributes in its cache, so you need to use filter:
var $li = $list.filter(function() {
return $(this).data('stuff') != undefined;
});
// do something with $li...
I think what you're looking for is:
$list.find('li').not('li[data-stuff]').addClass('foo');
The addClass()
is just there as a placeholder.