I’m having trouble understanding the workings of Javascript…I want to pass a form name and the submit button name so that the function can evaluate a passed value and either enable or disable the submit button.
It works if I explicitly use the names but I’d like it to work dynamically. Unfortunately I just am not understanding how to correctly use these arguments.
Right now the first document statement works (form name and button name are used), why doesn’t the second, where arguments are used? I know from the alert that the form name isn’t correct and can’t be used as is but this won’t work even if I explicitly name the form but use the passed button name which does appear correct.
I know this must be javascript 101 but I run into this time and again and am just not getting what’s what. Anyone willing to explain this to me?
<script type="text/javascript">
function aTest(formName,objName,val){
if (val<5){
document.aForm.aBtn.disabled=false;
alert(formName+" - "+objName);
}
else {
document.formName.objName.disabled=true;
}
}//end function aTest(formName,objName)
</script>
<form name="aForm">
<input name="testFld" type="text" onBlur="aTest(this.form,'aBtn',this.value)">
<input name="aBtn" type="submit" value='submit'></form>
Thank you for your kind attention!
Try:
When you need to access a property of an object by the value of some expression instead of with a constant, you use the square bracket operator. The expression within brackets is evaluated, and its value is then treated as a property name to look for.
Now, it would probably be wise to make sure that “formName” really is the name of a form:
Or more idiomatically: