several div with same dom id

J

Jean-Sébastien

Hello,

Is it possible to have a div component, that i repeat several time
on an html page : all those occurences of the div with the same dom
id.

thanx.
 
S

shimmyshack

Hello,

Is it possible to have a div component, that i repeat several time
on an html page : all those occurences of the div with the same dom
id.

thanx.

yes, but you will have a hard time referencing them, id's should be
unique, obviously!
switch to a classname instead, and style accordingly and then
reference the divs by their classname, unless you need to reference by
ID from some reason in which case you component should give them
unique ids.
 
A

ASM

Jean-Sébastien a écrit :
Hello,

Is it possible to have a div component, that i repeat several time
on an html page : all those occurences of the div with the same dom
id.

Tout est possible ! Le brouteur ne sera pas géné.

No you can't :
- not allowed by standards
- not usable : how to reach this or that div ?

If you want to copy a div and use this copy, you have to set it a new id
before to insert it

var idx = 0;
function addDiv() {
var d = document.getElementById('template').cloneNode(true);
d.id += '_'+idx;
document.body.appendChild(d);
idx++;
}
 
J

Jean-Sébastien

Jean-Sébastien a écrit :



Tout est possible ! Le brouteur ne sera pas géné.

No you can't :
- not allowed by standards
- not usable : how to reach this or that div ?

If you want to copy a div and use this copy, you have to set it a new id
before to insert it

var idx = 0;
function addDiv() {
var d = document.getElementById('template').cloneNode(true);
d.id += '_'+idx;
document.body.appendChild(d);
idx++;

}

merci
thank you
:)
 
T

Tim Slattery

Jean-Sébastien said:
Hello,

Is it possible to have a div component, that i repeat several time
on an html page : all those occurences of the div with the same dom
id.

Yes, but you won't be able to access them from script. The DOM assumes
that IDs are unique within a page. If there are more than one element
with the same ID, document.getElementById() will return the first
element with the requested ID. In order to get all of the DIVs, you'd
have to call document.getElementsByTagName, and cycle through the
array that function returns.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top