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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy getElementsByName() question

    After some severe experimentation I got to googling for an answer and stumbled upon these forums.

    So my big question is, is it possible to use getElementsByName() with a variable as the parameter? And if not what is another way I can get at each element based on a variable list of names.

    Code:
    var curObjSet = document.getElementsByName(String(idStack[y])).item(0)
    I'm trying desperately to make my code organic and not to have to hard code field names but without this working its leaving me with out options.

    Thank you for any time and help provided.

    - Eric

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    depending on what's in idStack[y], it should be fine.

    i don't remember .item(0), perhaps [0] would work better.

    also, it's probably better to use ID since some browsers are screwy with the name property.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Iyou use ID then getElementsByName() won't work if the name and ID aren't the same.

    And I dunno how getElementsByName() behaves on a set of radio buttons. Is it the same in all browsers??

    Ahhh...w3schools says that indeed you get one reference to the entire set, with the set converted to an array:
    http://www.w3schools.com/htmldom/met...entsbyname.asp

    Okay, consistent, at least.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Old Pedant View Post
    Iyou use ID then getElementsByName() won't work if the name and ID aren't the same.

    And I dunno how getElementsByName() behaves on a set of radio buttons. Is it the same in all browsers??
    .
    input names are probably pretty cohesive between browsers. but name's behavior on non-form elements is inconstant, notably so in IE.

    if you need to grab a few non-form elements, use document.getElementById in conjunction with unique ids.

    if you need several, or a couple groups of non-form elements, use class attribs and the ubiquitous getElementsByClass, available in the "post a javascript" section.


    if you just want to grab groups of inputs, i apologize for any confusion, getElementsByName should work just fine for that.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Tags for this Thread

    Posting Permissions

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