By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,245 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,245 IT Pros & Developers. It's quick & easy.

Safari bug? Error when I have more then one element with the same id

P: 11
This cannot be right.

I have two forms on one page. Both forms have an input element with the id="lastname". When I use a javascript to access theese input elements, I have to use

document.forms["form1"]["lastname"]

In IE, Firefox, and Opera, this works fine, because the two input elements are inside two different forms.
BUT: It does not work fine in safari.
What I do not understand is that Safari would not allow two elements with the same id in two DIFFERENT forms.
Nov 7 '07 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
You can have two elements with the same name, but an ID should be unique.
Nov 7 '07 #2

P: 11
Unique ID?

This is not only a form-input problem, but all elements on a page, Elements that cannot contain the name attribute (like <div>).

So thats why its not good enough. Many webpages, and more to come, are build up by external data, or by multiple webpages, created by different persons. So, when a javascript wants to access an element, it should be able to do so, if the scripts knows where to look. Like my example, the script knows it should look inside form1, and finds the element, in all major browsers except Safari...
Nov 7 '07 #3

acoder
Expert Mod 15k+
P: 16,027
If you give your input element the name "lastname", document.forms["form1"]["lastname"] should work.
Nov 7 '07 #4

drhowarddrfine
Expert 5K+
P: 7,435
As acoder said, ids are unique names to one element. If you must apply the same style/name to more than one element, use 'class'.
Nov 7 '07 #5

P: 11
OK I get that.

But it does not resolve my problem.

Lets say you have an application wich are build up by several DIVSs. Inside every DIV you have several SPANs, and you want to change the innerHTML of one or more of the SPANs. The SPANs has id attributes, but the id can be the same as an id inside one of the other DIVs.

This is easy, because you will access the SPAN you wish, by first accessing the DIV and then access the SPAN inside that DIV.

And this is what is working on IE Firefox and Opera but NOT in Safari.
Nov 9 '07 #6

acoder
Expert Mod 15k+
P: 16,027
Lets say you have an application wich are build up by several DIVSs. Inside every DIV you have several SPANs, and you want to change the innerHTML of one or more of the SPANs. The SPANs has id attributes, but the id can be the same as an id inside one of the other DIVs.

This is easy, because you will access the SPAN you wish, by first accessing the DIV and then access the SPAN inside that DIV.
An ID should be unique otherwise document.getElementById() is undefined.

If you want spans within a div try:
Expand|Select|Wrap|Line Numbers
  1. divEl.getElementsByTagName("span")
Nov 9 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.