473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,322 developers and data experts.

Browser Bug: document.getElementById() gets the wrong element (IE)

acoder
16,027 Expert Mod 8TB
Problem
document.getElementById() selects an element by its name.

Browser
Internet Explorer

Example
Relevant HTML code:
[HTML]<input name="test" type="text" value="test">[/HTML]
The Javascript code:
Expand|Select|Wrap|Line Numbers
  1. var obj = document.getElementById("test");
This will not result in an error and the element is available as 'obj' even though no element with the id "test" exists on the page.

Solution
This is a bug which will cause problems in most, if not all, other browsers. document.getElementById() should get an element by its ID as the name suggests, not by its name. This will also cause problems in IE when you have an element with a name the same as the ID of the element you actually want to access. The wrong element will be accessed!

Be careful how you name your elements. Do not use the same name as the ID of another element.

More Bugs, Quirks and Inconsistencies
Aug 18 '07 #1
0 7430

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

Similar topics

2
by: Eric Pickup | last post by:
I'm having a problem with the following html (I've simplified it down to a simple test case). Could anyone point out why Mozilla is having problems with this (I've tried so many variations that I...
6
by: John Ramsden | last post by:
.... when the id 'junk' doesn't exist anywhere in the document, instead of returning 'object'?! I am using Javascript for a drop-down menu, slightly adapted from one by Angus Turnbull (see...
1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
5
by: wk | last post by:
hi, in my code document.all.value works but document.getElementById.value fails. any ideas why?
7
by: PaulB | last post by:
Good Morning everybody, I'm trying to adapt a tutorial script that will handle the behaviour of an "Expanding/Contracting" site-navigation menu. The code that seems to handle the expansion and...
19
by: vunet.us | last post by:
Hello, My AJAX application paints data into about 500 cells with unique ID every 10 seconds. I am using document.getElementById() to find the right cell. However, I have noticed that...
0
acoder
by: acoder | last post by:
Problem The select object's add method doesn't append options to the end of the list. Browser Internet Explorer Example The Javascript code for appending an option element at the end of a...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
3
by: Muddasir | last post by:
Hello guyz Can you please help me with this. I am having problems with document.getElementById. It is working fine in FireFox but in IE its not working. What should i do. Thanks
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.