Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass a "control" name to a function?

Status
Not open for further replies.

reinholdj

Programmer
Jul 19, 1999
5
US
I have several functions that are somewhat redundant, and I would like to get rid of multiple functions by passing the "control" that the function is operating on as a parameter.

For example, currently we have:

function x1(data)
{
window.document.myForm.text1.value = data;
}

function x2(data)
{
window.document.myForm.text2.value = data;
}


What I would like to do is this:

function x(data,control)
{
window.document.myForm.[control].value = data;
}

But when I try that I get an error saying that it is not an object.

How do I do this?

Thanks,
John
 
You can pass the value of this as a parameter to a function from an onClick or onChange event, and it will pass a reference to the form object. Then you can say this.value. Or, you can pass a number or character string to the function and use that within the function. Ex:
Code:
onClick="x(this)"
onClick="x(1)"
onClick="x('string')
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 

if you're just passing the name of the control, you need to do:
Code:
function x(data,controlname)
{
  window.document.myForm[controlname].value = data;
}
(note the missing fullstop cf your code)

or as above, if the function is called from an event attached to the control, if you can pass a reference to the actual control using the "this" keyword like this:
Code:
onClick="x(this, data)"
then your function just needs to be:
Code:
function x(control, data)
{
  control.value = data;
}

any help?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top