Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Chicago, Illinois
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy getElementById not returning a value

    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>

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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
    Last edited by mrhoo; 12-10-2010 at 08:38 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    And FWIW, MSIE (other than maybe MSIE 9) doesn't support getElementsByName().

    But this code, for example, does work in FF:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    delonix (12-11-2010)

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Chicago, Illinois
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Old Pedant View Post
    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 ...
    Last edited by Kor; 12-11-2010 at 10:14 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •