...

View Full Version : passing name of field to javascript function and getting its value



garrisonian14
05-29-2009, 12:50 PM
I want to pass name of form field to a JavaScript function and get its value

like

function getValue(fldName)
{

sel_val = document.mcqReport.fldName.value;

}

Here mcqReport is formname
and i call this function like

getValue("txtUserName");
getValue("txtUseEmail");

txtUserName and txtUseEmail are two input fields of my form. But it gives error

Error: document.mcqReport.fldName is undefined

Please can you help me how i can do this.

Thanks

venegal
05-29-2009, 12:57 PM
It's undefined because there is no field with the name 'fldName'. In order to use the value of the variable fldName, and not the literal string 'fldName', you have to use


sel_val = document.mcqReport[fldName].value;

garrisonian14
05-29-2009, 01:04 PM
Thanks Dear.

What if i am using an arry. Like if fld is an array of check boxes.
will i access its values in document.frmReport[fld][0].value ??


It's undefined because there is no field with the name 'fldName'. In order to use the value of the variable fldName, and not the literal string 'fldName', you have to use


sel_val = document.mcqReport[fldName].value;

venegal
05-29-2009, 01:15 PM
If fld is an array containing the names of your checkboxes, and 'frmReport' is the name of the form, you can access the first one with


document.frmReport[fld[0]].value



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum