HighDots Forums  

Need help accessing radio button in function ...

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Need help accessing radio button in function ... in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
J Mox
 
Posts: n/a

Default Need help accessing radio button in function ... - 11-05-2005 , 01:31 AM






I have a function that changes which radio button is selected. I need to pass the form name to the
function but not the field name and am doing so like:

changeRadio(this.form);

The function:

changeRadio(formname){

// I have skipped how the variable fieldname is created all that is important is that it needs to be
created in the function, not passed to it, thus the need (I think) for the following

var radio2check = eval(formname+'.'+fieldname); // Don't work
var radio2check = eval("document.actualformname."+fieldname); // Works

// later in the function the above is used like:

radio2check[0].checked = true;

}

So in other words, when I manually enter the form name in the eval part it works fine but when I use
the formname containing this.form passed into the function it don't work. I need to know how to
access the radio button by combining the formname passed into the function and the fieldname
variable created in the function.



Reply With Quote
  #2  
Old   
J Mox
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-05-2005 , 01:42 AM






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.






Reply With Quote
  #3  
Old   
RobG
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-05-2005 , 07:44 AM



J Mox wrote:
Quote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);
Eval is almost never, ever needed - follow the link from the FAQ on
square bracket notation:

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>


A guess at what you might need is:

var radio2check = document.forms[formname].elements[fieldname];


Quote:
and it appears to work. If anyone has a better solution, please let me know.
Your eval method may work, but it is less than optimal.


--
Rob


Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-05-2005 , 08:40 AM



J Mox wrote:
Quote:
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.
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.




Reply With Quote
  #5  
Old   
J Mox
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-07-2005 , 12:30 AM



"RobG" <rgqld (AT) iinet (DOT) net.au> wrote

Quote:
J Mox wrote:
I tried changing

var radio2check = eval(formname+'.'+fieldname);

to

var radio2check = eval('document.'+formname.name+'.'+fieldname);

Eval is almost never, ever needed - follow the link from the FAQ on square bracket notation:

URL:http://www.jibbering.com/faq/faq_not..._brackets.html


A guess at what you might need is:

var radio2check = document.forms[formname].elements[fieldname];



and it appears to work. If anyone has a better solution, please let me know.

Your eval method may work, but it is less than optimal.


--
Rob
Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
thought I was in effect doing when I passed this.form to the function, then I think your solution
would work but since I am passing what I have now come to understand is a object referencing a form
the following works.

var radio2check = formname[fieldname];




Reply With Quote
  #6  
Old   
J Mox
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-07-2005 , 12:30 AM



"Richard Cornford" <Richard (AT) litotes (DOT) demon.co.uk> wrote

Quote:
J Mox wrote:
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.

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.


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.




Reply With Quote
  #7  
Old   
RobG
 
Posts: n/a

Default Re: Need help accessing radio button in function ... - 11-07-2005 , 01:21 AM



J Mox wrote:
[...]
Quote:
Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
thought I was in effect doing when I passed this.form to the function, then I think your solution
would work but since I am passing what I have now come to understand is a object referencing a form
the following works.

var radio2check = formname[fieldname];
You are passing a reference to the form object.

Glad to help. :-)


--
Rob


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.