Getting the value of one textbox in an array.

A

Andyza

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
 
M

Martin Honnen

Andyza said:
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);" ...>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top