PDA

View Full Version : trouble with form element via function argument


dewcansam
11-11-2002, 08:32 PM
Alright I've searched javascript.com, javascriptkit.com, and the forum and can't seem to find exactly what I am after.
So "What are you after" you ask. Well I would like to reference a form element by an argument passed to a function. Can this be done? What is the correct syntax if it can be achieved? Or maybe you have a better way to do this?

Code snip
[code]
<script>
function checkTheBox (someArg) {
document.myForm.someArg.checked = 1;
}
</script>
<form name=myForm>
<input type=button value="Click Me" onClick="checkTheBox('c01');">
<input type=checkbox name="c01">c01<br>
</form>
[code]

beetle
11-11-2002, 09:26 PM
close...using the full dot-syntax like you havedocument.myForm.someArg.checkedIs looking for an element with literally the name "someArg". To use the value of a string variable, we need to look it up through the form's elements collectiondocument.myForm.elements[someArg].checkedHowever, if all your function does is check the checkbox, you can do this instead (if you like)<input type=button value="Click Me" onClick="this.form.c01.checked=1;">
<input type=checkbox name="c01">c01<br>

dewcansam
11-11-2002, 09:32 PM
UGHH !!! I can't believe that i forgot that you could reference a form element through the elements array value. (I knew form.element[1] )
Thanx alot. Sorry if that was an inconvience .