jQuery : select all element with custom attribute

JavascriptJqueryJquery Selectors

Javascript Problem Overview


> 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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

As described by the link I've given in comment, this

$('p[MyTag]').each(function(index) {
  document.write(index + ': ' + $(this).text() + "<br>");});

works (playable example).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArianView Question on Stackoverflow
Solution 1 - JavascriptnnnnnnView Answer on Stackoverflow
Solution 2 - JavascriptShinTakezouView Answer on Stackoverflow