Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting radio buttons

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Does anybody know how to detect which radio button has been selected onSubmit?<br>
<br>
I would like all of the radio buttons to have the same, so they behave like they are supposed to<br>
<br>
thanks <p>theEclipse<br><a href=mailto: > </a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>
 
You could use the <i>onclick</i> attribute to set a variable like this:<br>
<br>
<i><br>
&lt;script&gt;<br>
...<br>
var fruitselection;<br>
&lt;/script&gt;<br>
...<br>
&lt;input type=&quot;button&quot; name=&quot;peaches&quot; onclick=&quot;fruitselection='peaches'&quot;&gt;<br>
&lt;input type=&quot;button&quot; name=&quot;pears&quot; onclick=&quot;fruitselection='pears'&quot;&gt;<br>
&lt;input type=&quot;button&quot; name=&quot;plumbs&quot; onclick=&quot;fruitselection='plumbs'&quot;&gt;<br>
...<br>
</i><br>
You would need to make sure you figure out a way that the variable's value is included in the selection (you could use JavaScript to do the submission, that would work). <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
Sorry, I mislead you, somewhat. You can use the above, but you couldn't use JavaScript to submit the information. You would still need to use a regular submit button or something. What you would need to do is create a hiddn input type and give it the value of <i>fruitselection</i>, like the following.<br>
<i><br>
&lt;script&gt;<br>
<br>
...<br>
function whatfruit()<br>
{<br>
...<br>
document.theform.thefruit.value = fruitselection;<br>
...<br>
}<br>
<br>
...<br>
&lt;/script&gt;<br>
...<br>
<br>
&lt;form name=&quot;theform&quot; action=&quot;?????&quot; method=post enctype=&quot;text/plain&quot; onsubmit=&quot;whatfruit()&quot;&gt;<br>
&lt;input type=&quot;hidden&quot; name=&quot;thefruit&quot; value=&quot;none&quot;&gt;<br>
...<br>
</i><br>
This should work. <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
although you gave a good answer, you did not quite get at what I wanted, but I also noticed that I left out an important word in my original post.<br>
<br>
I stated:<br>
<br>
&quot;I would like all of the radio buttons to have the same, so they behave like they are supposed to,&quot;<br>
<br>
what I meant was:<br>
<br>
I would like all of the radion buttons to have the same NAME, so they behave like a set of radio buttons.<br>
<br>
below is a discription of my situation.<br>
<br>
I have a form, with all fields hidden, containing all attributes to login to my web-based e-mail accounts.<br>
<br>
I have another form, with two radio buttons, one for each login. when you click the radio button, RB1, Javascript fills the hidden form with the correct information for that login. and the same goes for RB2. When you submit the form, it actually submits the hidden form, instead, using loginForm.submit();<br>
<br>
I want the radio buttons to have the same name, but do different actions.<br>
<br>
If I use the same as what you stated above, but with all of the name=&quot;&quot; attributes the same; and have different onClick values, will it work that way?<br>
<br>
I have also tryed to detect which radio button was clicked, that did not work.<br>
<br>
thanks, <p>theEclipse<br><a href=mailto: > </a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>
 
Yes it will. You would then reference each of the radio buttons in an array if needed and I would suggest giving them all the same name otherwise you will have to deal with the <i>elements</i> array which can get quite confusing. If you associate the variable's value with a function (by using <i>onSubmit=&quot;func(value)&quot;</i>) you can change the action attribute by referencing <i>document.theform.action=???</i>. You can also change the encoding, method, length, and the target in this manner (I named the properties their real names, so just substitute action for any of the names). <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
so, I would refrence to a single radio button like this:<br>
<br>
document.theForm.theNameRB[0]<br>
and the checked value like this:<br>
document.theForm.theNameRB[0].checked<br>
<br>
correct?<br>
<p>theEclipse<br><a href=mailto: > </a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top