View Full Version : Square Bracket Object Referencing
thepeskykid
06-27-2003, 05:14 PM
Any variable that holds a string value can be used between the square brackets to form a property accessor.
JavaScript Square Bracket Notation (http://www.litotes.demon.co.uk/js_info/sq_brackets.html#vId)
Why doesn't the following function work?
function validateFormFields(whichForm,whichField,fieldValue)
{
switch(whichForm)
{
case "income_data_input" :
validateIncomeFields(whichField,fieldValue);
break;
} // switch
document.forms[whichform].elements[whichField].value = fieldValue;
} // end validateFormFields
Note: validateIncomeFields(whichField,fieldValue) returns the value of fieldValue
Thank you
:)
beetle
06-27-2003, 05:25 PM
impossible to say without knowing what values you are passing into the function's parameters.
mordred
06-27-2003, 06:55 PM
thepeskykid, what you have does work - as long as you're passing strings that correspond to the form element's names.
Unless you are expecting it to do something else and hide that under the statement "it doesn't work"... so maybe you have to define for us what *exactly* went wrong and what the expected result should have been. ;)
beetle
06-27-2003, 06:58 PM
I see something
validateIncomeFields(whichField,fieldValue);
You say that it returns the value of fieldValue -- okay, so you return it but don't capture the return value into anything! I suspect you want to do this
fieldValue = validateIncomeFields(whichField,fieldValue);
??
thepeskykid
06-27-2003, 11:39 PM
sorry for slow response
onBlur="validateFormFields(document.income_data_input.name,this.name,this.value)"
calls...
validateFormFields(whichForm,whichField,fieldValue)
document.income_data_input.name == "income_data_input"
this.name == "you"
this value == value entered into text field...
so I thought that as whichForm, whichField and fieldValue are passed throughout the functions and returned back when tested using alerts, then...
document.forms[whichform].elements[whichField].value = fieldValue;
would be the same as...
document.forms["income_data_input"].elements["you"].value = fieldValue;
instead I get error...
document.forms.whichform not a valid object
I am trying to apply the previous advice by making the function more modular, passing the function the values they need, etc - maybe I have misunderstood - thanks for your help so far :)
kansel
06-28-2003, 09:58 AM
Usually I wouldn't notice this kind of thing at 4am but you appear to have a case conflict in your code:
function validateFormFields(whichForm,whichField,fieldValue)
document.forms[whichform].elements[whichField].value = fieldValue;
should this not be document.forms[whichForm]
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.