jQuery : select all element with custom attribute

Arian picture Arian · Nov 15, 2012 · Viewed 125.6k times · Source

Possible Duplicate:
jQuery, Select by attribute value, adding new attribute
jQuery - How to select by attribute

please consider this code:

<p>11111111111111</p>
<p MyTag="nima">2222222222</p>
<p>33333333333</p>
<p MyTag="Sara">>4444444444</p>

how I can select All p tag with attribute MyTag?

thanks

Answer

nnnnnn picture nnnnnn · Nov 15, 2012

Use the "has attribute" selector:

$('p[MyTag]')

Or to select one where that attribute has a specific value:

$('p[MyTag="Sara"]')

There are other selectors for "attribute value starts with", "attribute value contains", etc.