Tuesday, March 26, 2013

jQuery Selector Using Data Attribute


<div id="divId" data-more-info="data value">
    Some div content
</div>


The div element above has two attributes declared. The first one is the id attribute and the second one is the data-more-info attribute.

1. The selector by id for the div above is pretty easy:
$("#divId")

2. Now, here's a way to select the div above using the second attribute data-more-info:
$("div[data-more-info='data value']")


The second query returns all div elements with 'data value' as value for its data-more-info attribute.

No comments:

Post a Comment