...

View Full Version : getElementById not returning a value



delonix
12-10-2010, 06:41 PM
None of my getElementById (or other) references produce anything but "undefined." This occurs in the latest versions of Fx, IE, Chrome, and Safari, so it must be something I am doing but not not seeing here. I have boiled it down to the following without success, and wonder whether there is some add on interaction. Fx 3.6.12 has the problem with this code:

<code>
<html>
<head>
<title>This is a test of getElementById</title>
</head>
<body>
<div id="area" name="area_name">xxx</div>
<script type="text/javascript">
var divs = document.getElementsByTagName('div');
var msg = '';
msg = 'Divs[0] contains ' + divs[0] + ' with width = ' + divs[0].width + "\n";
msg +='Test area width by id = ' + document.getElementById('area').width + "\n";
msg +='Test area width by name = ' + document.getElementsByName('area_name').width;
alert(msg);
</script>
</body>
</html>
</code>

mrhoo
12-10-2010, 07:34 PM
undefined in this case refers to 'width', the div does not have a property named 'width'.

The id call is correct, but document.getElementsByName returns a collection and should be-

document.getElementsByName('area_name')[0]

You might try testing offsetWidth, which is a property of all displayed elements

Old Pedant
12-10-2010, 08:04 PM
And FWIW, MSIE (other than maybe MSIE 9) doesn't support getElementsByName().

But this code, for example, does work in FF:


<html>
<head>
<title>This is a test of getElementById</title>
</head>
<body>
<div id="area" name="area_name" style="width: 431px;">xxx</div>
<script type="text/javascript">
var divs = document.getElementsByTagName('div');
var msg = '';
msg = 'Divs[0] contains ' + divs[0] + ' with width = ' + divs[0].style.width + "\n";
msg +='Test area width by id = ' + document.getElementById('area').style.width + "\n";
msg +='Test area width by name = ' + document.getElementsByName('area_name')[0].style.width;
alert(msg);
</script>
</body>
</html>

If you omit the code in blue, it works in MSIE.

delonix
12-11-2010, 01:16 AM
I thank you gentlemen hugely. The value is "offsetWidth" for the ID reference. I really seldom use the name= array and was sloppy.

Dreamweaver "aided" me by proffering "width" and so I did not RTFM as I usually do, also being ein alterkacker. Have you ever noticed that at work the interruptions make errors so much more common than working quietly at home in a profitable calm?

We are moving in a few years to Ocean Shores, WA, and you will be welcome to a razor clam feast someday there. Thanks for the heads up on the "by name" issues, too, which I had put in to see what happened.

Again, you saved my day!

Kor
12-11-2010, 09:09 AM
And FWIW, MSIE (other than maybe MSIE 9) doesn't support getElementsByName().
Not really. All the browsers support document.getElementsByName(), but only for the form's elements. The W3C Recommendations stand for using the id identifier instead of name, but with the notable exception of the form's elements, which need a name by all means, for the submit reasons. Or, if an element can legally bear a name, it is to be found on using document.getElementsByName(), IE included. But as a DIV can not bear a name, it seems logical that it can not be found with that method. If a browser still allows that, it is the exception, not the rule :)

Moreover, all the browsers traditionally keep the possibility of using the name for other type of elements, like images, links, anchors, frames, iframes, maps ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum