SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementsByName not returning anything in IE

    Site

    Code:
    function fnn(id) {
        var i=0;
        for (i=0;i<=50;i++) {
            document.writeln('<span name="'+i+'" onmouseover="increase(this,'+i+')" onmouseout="decrease(this,'+i+')">&nbsp;'+fulldata[i][id]+'</span><br/>');
        }
    }
    
    function increase(thing, thingName){
        var node = null;
    
        if (document.getElementsByName) {
          node = document.getElementsByName(thingName);
        } else if (document.all) {
          node = document.all[thingName];
        } else if (document.layers) {
          node = document.layers[thingName];
        }
        
        var i=0;
        for(i=0; i<node.length; i++) {
            node.item(i).style.color = "white";
            node.item(i).style.fontSize = "medium";
        }
    
        thing.style.fontSize = "large";
    }
    HTML Code:
    <div class="leftCont">
            Shady Grove
            <p><script type="text/javascript">
               <!--
               fnn(0);
               // -->
            </script></p>
        </div>
    In IE7, node.length in the increase() function is always 0, however it works correctly in FF.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The actual DOM compliant function is getElementsByTagName().

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,833
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.getElementsByName() is perfectly valid but is not equivalent to the now obsolete document.all (IE4) and document.layers (Netscape 4). document.getElementsByTagName() is the equivalent to those.

    The problem though is that you are trying to reference node.item(i) which IE doesn't populate. Try using node[i] instead which does work for all browsers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed it to:
    Code:
     if (document.getElementsByName) {
          node = document.getElementsByName(thingName);
        } else if (document.getElementsByTagName) {
          node = document.getElementsByTagName(thingName);
        }
        
        var i=0;
        for(i=0; i<node.length; i++) {
            node[i].style.color = "white";
            node[i].style.fontSize = "medium";
        }
    node.length is still always 0 in IE.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,833
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If I remember correctly, a name field must start with a letter or underscore. Also name attributes are not valid on span tags.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first condition in your changed code doesn't make sense. getElementsByName returns a list of elements with name attribute=what you specify, whereas getElementsByTagName returns elements which have tagname=what you specify:

    Code:
    <input type="text" name="myInputField">
    ..
    var a = document.getElementsByName("myInputField");
    var b = document.getElementsByTagName("input");


Bookmarks

Posting Permissions

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