Getting the value of one textbox in an array.

Discussion in 'Javascript' started by Andyza, Aug 7, 2008.

  1. Andyza

    Andyza Guest

    I have a form with a number of textboxes. Each textbox has the same
    name.

    <input type="text" name="txtTest" value="1" onclick="GetValue()">
    <input type="text" name="txtTest" value="2" onclick="GetValue()">
    <input type="text" name="txtTest" value="3" onclick="GetValue()">
    <input type="text" name="txtTest" value="4" onclick="GetValue()">
    <input type="text" name="txtTest" value="5" onclick="GetValue()">

    To loop through the textboxes and retrieve the value of each box I use
    this loop:

    <script language="javascript">
    function GetValue() {
    for (i=0; i<document.forms[0].txtTest.length; i++){
    alert(document.forms[0].txtTest.value)
    }
    }
    </script>

    GetValue() gives me an array with the values of all the textboxes. How
    do I get the function to return JUST the value of the textbox that I
    clicked on?

    Example, if I click on the third textbox it will display an alert with
    3 and if I click on the first textbox it will display an alert with 1.

    Thanks
    Andyza, Aug 7, 2008
    #1
    1. Advertising

  2. Andyza wrote:

    > GetValue() gives me an array with the values of all the textboxes. How
    > do I get the function to return JUST the value of the textbox that I
    > clicked on?


    function getValue (input) {
    alert(input.value);
    }

    <input onclick="getValue(this);" ...>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 7, 2008
    #2
    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. Dan Davenport via .NET 247

    Bind optional one-to-one relationship to textbox

    Dan Davenport via .NET 247, Aug 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    632
    Dan Davenport via .NET 247
    Aug 4, 2004
  2. AGSHAR
    Replies:
    1
    Views:
    591
    AGSHAR
    May 31, 2008
  3. agb

    writing two textbox's text in one textbox

    agb, Aug 26, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    166
    Ken Cox [Microsoft MVP]
    Aug 27, 2003
  4. ErwinP
    Replies:
    1
    Views:
    734
    ErwinP
    Aug 19, 2005
  5. Steve
    Replies:
    4
    Views:
    208
    Steve
    Feb 7, 2006
Loading...

Share This Page