mix mix - 6 months ago 26
CSS Question

Find HTML Element that has Specific Attribute Using jQuery

How can I target an HTML element that has a specific attribute, but when that attribute has no value (well, whether it has a value or not). I've found others asking similar questions about elements with attribute values but not based on existence of the attribute itself.

For example, with this HTML:

<div id="id1">
<div class="c1"></div>
<div class="c2" an-attribute ></div>
<div class="c3"></div>
</div>


How might I go about finding the
div
within
#id1
that has the attribute
an-attribute
? I can't count on it being in a particular location related to the other
divs
but can count on it being in
#id1
.

Answer

Try this.

$('#id1 div[an-attribute]');