Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Traversing DOM

  • 22-08-2010 08:29PM
    #1
    Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭


    I'm looking to get the value of an input field when another field has the same name. I need to do it relative to the form it's apart of. I'm sure it's easy to do but I just can't get it right, any help would be appreciated :).

    E.g.
    <form id="form1">
       <input type="text" name="some_name" />
    </form>
    
    <form id="form2">
       <input type="text" name="some_name" />
    </form>
    

    I need to go [ document -> form2 -> some_name ] as oppose to [ document -> some_name[1] ]

    I've tried
    document.getElementById("form2").getElementsByName("some_name").value;
    document.getElementById("form2").childElements("some_name").value;
    
    and similiar ways, none of which work.

    Any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 1,505 ✭✭✭viking


    document.getElementById('form2').some_name.value


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    Knew it had to be easy but for feck sake, now I feel like an idiot [and rightly so].

    Thanks.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    You could actually give the two input fields unique "ids", and still keep the names
    <form id="form1">
       <input id="form1input" type="text" name="some_name" />
    </form>
    
    <form id="form2">
       <input id="form2input" type="text" name="some_name" />
    </form>
    

    Then you could use
    document.getElementById("form1input").value
    document.getElementById("form2input").value
    


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    I did think of doing that but wanted to keep the HTML as simple as possible, plus I knew their must've been an easy way to get the input field by name and it's handy to know for future reference (I still can't believe I couldn't figure it out on my own, couldn't even find it on Google [got to work on those Googling skills :pac:]).


Advertisement