strange behaviour of getElementsByName and method name in IE and FF

Discussion in 'Javascript' started by Thomas 'PointedEars' Lahn, Nov 19, 2009.

  1. ..rhavin grobert wrote:

    > the following little test-markup doesnt work...
    > in firefox, i dont even get the names of the elements,
    > in IE, i get the names but the getElementsByName() returns an emty
    > array.
    > [...]
    > <li id="m1" name="menu" ><a href="#" onClick="ps_page('m1',
    > 'p1');">Daten</a></li>
    > <li id="m2" name="menu" ><a href="#" onClick="ps_page('m2',
    > 'p2');">Musik</a></li>
    > </ul></div>
    > <div id="p1" name="page" >
    > <p>div 1</p>
    > </div>
    > <div id="p2" name="page" >
    > <p>div 2</p>
    > </div>


    Neither LI elements nor DIV elements have a `name' attribute.

    <http://validator.w3.org/>


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Nov 19, 2009
    #1
    1. Advertising

  2. ..rhavin grobert wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> Neither LI elements nor DIV elements have a `name' attribute.

    >
    > Ok, thx for your answer; i cant even imagine what stupidity let the
    > w3c choose to deprecate 'name' -


    Blaming others for your mistakes is not going to get you any more good
    answers here.

    The W3C did not deprecate the `name' attribute for those elements; it was
    never part of the markup language to begin with. If you read the HTML 4.01
    Specification as I already recommended, you can see which elements have a
    `name' attribute and which do not (there is an "attributes" overview linked
    at the top).

    > do you perhaps have another suggestion for naming a couple of divs?


    Use the `id' attribute.

    > i need each div to have an unique id (the id) and a common name...


    Let's say you have elements with the following IDs:

    id1
    id2
    id3

    Then you can iterate over them as follows:

    var o, i = 1;
    while ((x = document.getElementById("id" + i)))
    {
    // ...
    i++;
    }

    It begs the question what do you think this should be good for, though. Your
    approach is most likely wrong. Menus should be implemented with nested
    lists (UL in LI in UL) so that the menu is based on semantic markup, thereby
    meeting accessibility guidelines and legislation. The Web is full of good
    CSS examples (those also work without scripting in most browsers) these
    days.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Nov 19, 2009
    #2
    1. Advertising

  3. the following little test-markup doesnt work...
    in firefox, i dont even get the names of the elements,
    in IE, i get the names but the getElementsByName() returns an emty
    array.

    it would be very nice if someone could have a look at it and tell me,
    what the hell is going on here;-(

    TIA, ~.rhavin;)
    _______________________________________________________________

    <html><head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />
    <script type="text/javascript" >

    function ps_page(menuID, pageID)
    {
    blur();
    alert('ps_page(' + menuID + ', ' + pageID+ ')');

    pageelm = document.getElementById(pageID);
    menuelm = document.getElementById(menuID);
    alert('names(pages): ' + pageelm.name + ', names(menues): ' +
    menuelm.name );

    pagegrp = document.getElementsByName(pageelm.name);
    menugrp = document.getElementsByName(menuelm.name);
    alert('size(pages): ' + pagegrp.length + ', size(menues): ' +
    menugrp.length );

    return true;
    }
    </script>
    </head><body>
    <div id="tabmenu"><ul>
    <li id="m1" name="menu" ><a href="#" onClick="ps_page('m1',
    'p1');">Daten</a></li>
    <li id="m2" name="menu" ><a href="#" onClick="ps_page('m2',
    'p2');">Musik</a></li>
    </ul></div>
    <div id="p1" name="page" >
    <p>div 1</p>
    </div>
    <div id="p2" name="page" >
    <p>div 2</p>
    </div>
    <p>test</p>
    </body><html>
    .rhavin grobert, Nov 19, 2009
    #3
  4. Re: strange behaviour of getElementsByName and method name in IE andFF

    On 19 Nov., 14:09, Thomas 'PointedEars' Lahn <>
    wrote:
    > Neither LI elements nor DIV elements have a `name' attribute.


    Ok, thx for your answer; i cant even imagine what stupidity let the
    w3c choose to deprecate 'name' - do you perhaps have another
    suggestion for naming a couple of divs? i need each div to have an
    unique id (the id) and a common name...
    .rhavin grobert, Nov 19, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    document.getElementsByName problem

    simon, May 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    26,232
    simon
    May 10, 2005
  2. AR
    Replies:
    2
    Views:
    178
  3. mitch
    Replies:
    3
    Views:
    247
    Richard Cornford
    Nov 14, 2005
  4. briggs
    Replies:
    20
    Views:
    440
    Randy Webb
    Jan 17, 2006
  5. acord

    getElementsByName()

    acord, Mar 13, 2006, in forum: Javascript
    Replies:
    1
    Views:
    82
    Michael Winter
    Mar 13, 2006
Loading...

Share This Page