Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to deal with a selective div having same id?

Status
Not open for further replies.

Weese

Programmer
Apr 3, 2003
17
My program is like this.

function fnGetNames(){
// Returns a collection with 2 INPUT type=text elements.
var aInput=document.getElementsByName("same")[0];
aInput.style.display="None";
}

<Div id=&quot;same&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;firstName&quot;>
</Div>
<Div id=&quot;same&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;firstName&quot;>
</Div>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Get Names&quot; onclick=&quot;fnGetNames()&quot;>

Working fine in IE but giving error in Netscape.
Any workaround.
 
change it!

you should never have two elements with the same id. the very definition of an id is a Unique identifier.

=========================================================
while (!succeed) try();
-jeff
 
There are some rare cases we need to go for same id.It is dynamically generated.Is there any workaround..
 
palayam;
jemminger is right. Id should only be used once. Class can be used many times. if you want, you could try this:-

<div id=&quot;var1&quot;>
</div>

</div id=&quot;var2&quot;>
</div>

now, you can get to both element by doing this:-

function alakazaam() {
for (i=1; i <= 2; i++) {
var aInput=document.getElementById(&quot;var&quot; + i);
aInput.style.display=&quot;None&quot;;
}
}
 
palayam,

I do not want to sound patronizing but there are no cases that require the same ID for numerous items. It goes against programming principles so if it happens it means you are doing something wrong somewhere (or that you are stuck with something rather complicated that requires help from someone).

A golden rule of programming to remember : ID's are unique.

If you have a situation where you are stuck and cannot figure out a way to deal with the problem just post it here and we'll show you how to do it properly! :)
Gary Haran
==========================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top