View Full Version : How to pass textbox name to function in javascript

12-27-2010, 11:48 PM
I have a php script which dynamically creates a table containing text boxes with variable names. Example:

echo '<td align="center" bgcolor="'.$bgcolor.'"><input type="text" name="payable_'.$key.'" id="payable_'.$key.'" value="'.$payable.'" size="3" maxlength="4" /></td> ';

In the above instance the text box name might be 'payable_23323'.

I need to code an onKeyUp event which sends the text box name to a function, and in that function I need to retrieve the text box name and the string after 'payable_' together with the value the user has input into the text box.

Any help is greatly appreciated . . . I have been Googling this for hours to no avail - probably because I don't know what to search for . . .


Old Pedant
12-28-2010, 12:07 AM
<input ... onkeyup="whatever(this);"/>

and then the function can do:

function whatever( fld )
var name = fld.name;
var value = fld.value;
var key = name.substring(8); // everything after "payable_"
alert("The field " + name + " has the value " + value + " and the key " + key);
// or whatever else you want to do with them

Read up on the use of this -- it's a really powerful little keyword.

12-28-2010, 12:51 AM
This solution is exactly what I needed. Thank you for your expert help!!