Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

getting .value from javascript nodecollection?

Back to Forum: Tech Off
  • User profile image
    jsampsonPC

    I have four radio buttons, all with the same Name value, but different Value values.

    <input type="radio" name="cars" value="vw" />
    <input type="radio" name="cars" value="dodge" />

    Etc.

    If there someway I can extract the value of the selected node from that list?

    document.getElementsByName("cars").value type of thing?

    I ended up just doing a loop, and looking for whichever node had .checked as true - but I'm hoping their is a non-loop way to do this.

  • User profile image
    Matthew van Eerde

    Nope, the loop is the correct way to do it.  You should also probably check !elems[i].disabled if you're writing a general-purpose function.

  • User profile image
    zproxy

    The problem is you are using name attribute but you should use distinct id attributes to reference them via getElementById().

    Example:

    <input type="radio" id=''cars0" name="cars[0]" value="vw" />
    <input type="radio" id="cars0" name="cars[1]" value="dodge" />

    just my 2 cents

  • User profile image
    stevo_

    Its against DOM to reference more than one element under the same ID. Not sure how you can expect browsers to consistently handle this, some may return an array of elements and other would just .. not.

  • User profile image
    jsampsonPC

    stevo_ wrote:
    Its against DOM to reference more than one element under the same ID. Not sure how you can expect browsers to consistently handle this, some may return an array of elements and other would just .. not.


    I'm not using ID's...I'm using the name attribute - just as DOM standards suggest, if I'm not mistaken.

  • User profile image
    Ang3lFir3

    jsampsonPC wrote:
    
    stevo_ wrote: Its against DOM to reference more than one element under the same ID. Not sure how you can expect browsers to consistently handle this, some may return an array of elements and other would just .. not.


    I'm not using ID's...I'm using the name attribute - just as DOM standards suggest, if I'm not mistaken.

    he was replying to zproxy.....

    the loop is the correct way AFAIK

  • User profile image
    stevo_

    Ang3lFir3 wrote:
    
    jsampsonPC wrote:
    stevo_ wrote: Its against DOM to reference more than one element under the same ID. Not sure how you can expect browsers to consistently handle this, some may return an array of elements and other would just .. not.


    I'm not using ID's...I'm using the name attribute - just as DOM standards suggest, if I'm not mistaken.

    he was replying to zproxy.....

    the loop is the correct way AFAIK


    Yes I was, sorry I have the admit the interface is a little confusing because- 'reply' is right next to the user information and is then obviously repeated for each post, I didn't notice and just presumed Reply would mean 'reply to this post'.

    Live n learn..

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.