Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Traversing DOM

Options
  • 22-08-2010 8:29pm
    #1
    Registered Users 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 Posts: 1,504 ✭✭✭viking


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


  • Registered Users 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 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