473,800 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help accessing radio button in function ...

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(thi s.form);

The function:

changeRadio(for mname){

// 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.
Nov 5 '05 #1
6 1863
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.

Nov 5 '05 #2
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.c om/faq/faq_notes/square_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
Nov 5 '05 #3
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.
Nov 5 '05 #4
"RobG" <rg***@iinet.ne t.au> wrote in message
news:43******** **************@ per-qv1-newsreader-01.iinet.net.au ...
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.c om/faq/faq_notes/square_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];
Nov 7 '05 #5
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:dk******** ***********@new s.demon.co.uk.. .
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.
Nov 7 '05 #6
J Mox wrote:
[...]

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
Nov 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12351
by: jason | last post by:
The following (likely far from imperfect code), reports a value of NaN in the j4 display. I suppose the problem is I am not really passing the "checked" value of the radio button via .value ... without having to get this value via html, is there any way I can passed the checked value via html .. maybe with syntax like n4.checked.value or something.. Many thanks. <html>
1
3441
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with ***********************, I've included all the code incase that isn't where the problem is. Any help would be hugely appreciated. Mick
5
3110
by: Digital Puer | last post by:
I have the following HTML form: - radio button A (default selected) - radio button B - input field, of type "file" with "Choose" button - submit button I would like to have it so that if the user clicks on the "Choose" button of the input field (to select a file on the local disk), then radio button B is automatically
2
4427
by: Steve Black | last post by:
Hello, I am dynamically creating checkboxes on my web page based on data in a SQL Server table. Therefore, these checkboxes do not exist at design time and I cannot refer to them in my server-side code at design time. Once the page is submitted, how can I access these checkboxes in my code-behind? Thanks for the help,
3
2074
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way (trick, custom RadioButton, or javascript) to make radio button (radio1) belong to 2 independent radio button groups instead of one? This would be an equivalent of sayting something like radio1.GroupName1 = "GroupA"; radio1.GroupName2 = "GroupB";
18
2271
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
9
2255
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script type="text/javascript"> function setCheckedValue(radioObj, newValue) { if(!radioObj)
7
1641
by: Franky | last post by:
Following discussion in a previous post, i have used an array inside a loop to gather results: //Function called from another page function displayQuestions($ModNum){ //modnum is used in the db query global $QuestionNums; if (!isset($_POST)){
7
4876
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio buttons and check box. I am adding two radio buttons once for a row.Now my problem is the radio buttons in all the rows that added dynamically are behaving as same group i.e when i am selecting a radio button in second row,radio button in first row...
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.