About This Code
Brief Description:
The JavaScript function getFieldValue
Contributor:
Andrew Jones
Last Modified:
17 Jun 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
This function will return the field value (or value list) based on the element type function getFieldValue ( theField, vType ) {
//this function will return the field value (or value list) based on the element type
theValue = "";
sep = "";
hits = 0;
//text is the user-entered value as a string
if ( vType == "text" ) return ( theField.value );
//textarea is the user-entered value as a string array of one element
if ( vType == "textarea" ) return ( theField[0].value );
//checkboxes & radio buttons are not so simple
if ( vType == "checkbox" || vType == "radio" ) {
if ( theField.value == null ) {
//if we're here, we are validating a radio button or a nn multi-element
checkbox
for ( i = 0; i < theField.length; i++ ) {
if ( theField[i].checked ) {
hits++;
if ( hits > 1 ) {
sep = "; ";
}
theValue += sep + theField[i].value;
}
}
}
return ( theValue );
} else {
//if we are here, must be an ie checkbox, or nn with a one-element
checkbox")
if ( navigator.appName == "Microsoft Internet Explorer" ) {
//ie. return some data so we can validate on the server;
return ("can't validate on client")
}
//nn one-element checkbox, see if its checked ...
if (theField.checked ) {
return ( theField.value );
} else {
return ( "" );
}
}
//select is an array of selection pointers to an array of strings
representing the choices
if ( vType == "select" ) {
for ( i = 0; i < theField.options.length; i++ ) {
if ( theField.options[i].selected ) theValue += theField.options[i].text
}
return ( theValue );
}
}
Usage / Example