By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

Getting the value of one textbox in an array.

P: n/a
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[i].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
Aug 7 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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/
Aug 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.