473,387 Members | 1,569 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,387 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 7446

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.