user2728841
user2728841

Reputation: 1427

JQuery find all data- elements

I'm data binding by adding a html5 data- custom attribute to an element

<div id="fred">
<input type="text" data-fld="Field1" value="10" />
<input type="text" data-fld="Field2" value="11" />
<input type="text" data-fld="Field3" value="12" />
<input type="text" value="13" />
</div>

I'm trying to search for all elements that have a data-fld specified.

col = div.find("[data-fld!='']");

But no luck with what I've tried this far.

Here's a jsfiddle for it. http://jsfiddle.net/p2KsP/4/

Upvotes: 2

Views: 6145

Answers (1)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263077

You only have to use a Has Attribute selector:

var col = $("#fred").find("[data-fld]");
console.log(col.length);  // 3

An Attribute Not Equal selector will not work because it matches elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value. It means [data-fld!=''] will end up matching all elements.

Upvotes: 2

Related Questions