| re: Need help accessing radio button in function ...
"Richard Cornford" <Richard@litotes.demon.co.uk> wrote in message
news:dkig88$fbq$1$830fa17d@news.demon.co.uk...[color=blue]
>J Mox wrote:[color=green]
>> I tried changing
>>
>> var radio2check = eval(formname+'.'+fieldname);
>>
>> to
>>
>> var radio2check = eval('document.'+formname.name+'.'+fieldname);
>>
>> and it appears to work. If anyone has a better solution,
>> please let me know.[/color]
>
> If the latter "works" then the variable (paramerter?) - formname - is
> not a string representation of a name but an object that has a property
> called - name -. It is also an object with the property - name - where
> that name happens to be the name of a FORM Element object. That is
> unlikely to be a coincidence and so I would deduce that - formname - is
> a reference to an object that is a FORM element object, indeed it is
> _the_ form element object. So some confusion has been introduced by
> giving it a name that actually conceals its real nature.
>
> If you do - eval('document.'+formRef.name); - what you get back is -
> formRef -, a reference to the FORM object that you started with, so you
> can skip that. if you do - eval('fromRef.'+fieldName) - you are doing
> the same as - formRef[fieldname] - but slower and more indirectly.
>
> var radio2check = formname[fieldname];
>
> - should "work" at least as effectively as the - eval - and is; shorter,
> simpler, faster and more direct (and so easier to debug and maintain).
> (but do change the - formname - variable name)
>
> A general rule for newcomers to javascript would be that if you are
> considering using - eval - then you have the opportunity to learn
> something new that will not use - eval - and be objectively better than
> the - eval - use.
>
> Richard.
>
>[/color]
Thanks, you helped me understand the difference between objects and properties. I was passing
this.form to the function as formname (I now see and agree how the variable was confusingly named)
mistakenly thinking that it resulted in the actual form name being passed to the function. I think I
now understand that passing this.form to a function passes a form element object from which form
properties can be accessed such as your simpler example which worked fine. |