How to select all elements with a particular ID in jQuery?

JqueryCss Selectors

Jquery Problem Overview


I'm trying to select all <div>s with the same ID in jQuery. How do i do it?

I tried this and it did not work

jQuery('#xx').each(function(ind,obj){
      //do stuff;
});

Jquery Solutions


Solution 1 - Jquery

Though there are other correct answers here (such as using classes), from an academic point of view it is of course possible to have multiple divs with the same ID, and it is possible to select them with jQuery.

When you use

jQuery("#elemid") 

it selects only the first element with the given ID.

However, when you select by attribute (e.g. id in your case), it returns all matching elements, like so:

jQuery("[id=elemid]") 

This of course works for selection on any attribute, and you could further refine your selection by specifying the tag in question (e.g. div in your case)

jQuery("div[id=elemid]") 

Solution 2 - Jquery

I would use Different IDs but assign each DIV the same class.

<div id="c-1" class="countdown"></div>
<div id="c-2" class="countdown"></div>

This also has the added benefit of being able to reconstruct the IDs based off of the return of jQuery('.countdown').length


Ok what about adding multiple classes to each countdown timer. IE:

<div class="countdown c-1"></div>
<div class="countdown c-2"></div>
<div class="countdown c-1"></div>

That way you get the best of both worlds. It even allows repeat 'IDS'

Solution 3 - Jquery

Your document should not contain two divs with the same id. This is invalid HTML, and as a result, the underlying DOM API does not support it.

From the HTML standard:

> id = name [CS] This attribute assigns > a name to an element. This name must > be unique in a document.

You can either assign different ids to each div and select them both using $('#id1, #id2). Or assign the same class to both elements (.cls for example), and use $('.cls') to select them both.

Solution 4 - Jquery

Try this for selecting div by first id

$('div[id^="c-"]')

Solution 5 - Jquery

$("div[id^=" + controlid + "]") will return all the controls with the same name but you need to ensure that the text should not present in any of the controls

Solution 6 - Jquery

Can you assign a unique CSS class to each distinct timer? That way you could use the selector for the CSS class, which would work fine with multiple div elements.

Solution 7 - Jquery

You could also try wrapping the two div's in two div's with unique ids. Then select the div by $("#div1","#wraper1") and $("#div1","#wraper2")

Here you go:

<div id="wraper1">
<div id="div1">
</div>
<div id="wraper2">
<div id="div1">
</div>

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
QuestionmarkView Question on Stackoverflow
Solution 1 - JquerymydoghaswormsView Answer on Stackoverflow
Solution 2 - JqueryBallsacian1View Answer on Stackoverflow
Solution 3 - JqueryAyman HouriehView Answer on Stackoverflow
Solution 4 - JqueryatabakView Answer on Stackoverflow
Solution 5 - JqueryNiranjan KumarView Answer on Stackoverflow
Solution 6 - JqueryPaul MorieView Answer on Stackoverflow
Solution 7 - JqueryomerView Answer on Stackoverflow