views:

3995

answers:

5

hei guys i want to select two divs 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;
});

i have jquery countdown timers in a page, and i update them via ajax. some of these countdown timers repeat and that is why i need to use the same id on the divs.

+24  A: 

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.

Ayman Hourieh
It should also be noted that most of the underlying getElementById() functions jQuery and other libraries end up calling to do the actual search refuse to return anything but the first (or random?) element matching a given ID, and supporting it would mean manually walking the DOM. Just not worth it for invalid markup!
ironfroggy
Regarding this post:Interestingly enough, there is a javascript function getElementsByName(xxx) - notice the plural - implying that it is perfectly legal to have more than one element with the same name...
Jimbo
@Jimbo - True but my answer is about elements with the same `id`, not `name`.
Ayman Hourieh
@Jimbo — well, yes, radio groups depend on the feature. Not all elements can have names though. The question mentions divs, and they don't have a name attribute.
David Dorward
+1  A: 

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.

Paul Morie
paul,no i cant use a unique class for each timer, because i use the class to select all timers, to initialize and style the countdown timer.
mark
**HTML** class. People use classes almost exclusively for matching CSS selectors doesn't make them part of a different language.
David Dorward
+14  A: 

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'

Ballsacian1
Ballasacian: i have n counters with ids 1..n, so for each i have id=c-1,id=c-2 etc. but some of these repeat. so i cannot use your method. and they all have the same class='countdown'thanks
mark
Why would you want it to repeat? Especially on an ID which has to be unique.
Ballsacian1
Ballasacian: thanks! that was brilliant! works like the way i want!! dont know why i did not think of that before!
mark
Glad to be of assistance.
Ballsacian1
A: 

I'm too loking for a solution for this. There are some Div's being generate automatically. I'need to reduce there height using some jquery. But the point is, there are two DIVs with same ID value. :(

bluepicaso
A: 

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")

oh my html is not showing. here you go: div id="wraper1"

div id="div1"

/div

div id="wraper2"

div id="div1"

/div

omer