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!

Select a radio by only clicking in text??????

Status
Not open for further replies.

ale77

Programmer
Joined
Jul 18, 2003
Messages
38
Location
US
Hi. I have a question. I know that in javascript a lot of things are possible, but I can't find a way to do this.

I have a simple form with radio buttons. My question is if there is a way that I can select the radio button by only clicking on the text. Example, selecting the radio option one by clicking with the mouse on the text "Option #1"?????????????

<table border=&quot;0&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; width=&quot;105&quot; >
<tr class=&quot;radio_button&quot;>
<td valign=&quot;top&quot;><span><b>Options:</b></span>
<span><input type=&quot;radio&quot; name=&quot;options&quot; value=&quot;ONE&quot; ID=&quot;Radio1&quot; checked >Option #1
<br><input type=&quot;radio&quot; name=&quot;options&quot; value=&quot;TWO&quot; ID=&quot;Radio2&quot; checked >Option #2
<br><input type=&quot;radio&quot; name=&quot;options&quot; value=&quot;THREE&quot; ID=&quot;Radio3&quot; checked >Option #3
</td></tr></table>
 
ale77,

Tested this in IE6 and NN7, set up a span and give it an
onClick event.

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>TEST</title>
<script language=&quot;JavaScript&quot;>
function ckrad(id){
id = parseInt(id);
d = document.forms[0];
d.grp1[id].checked = true;
}
</script></head><body><form>
<span id=&quot;0&quot; onClick=&quot;ckrad(this.id)&quot;>
<input type=&quot;radio&quot; name=&quot;grp1&quot;>Option1</span><br>
<span id=&quot;1&quot; onClick=&quot;ckrad(this.id)&quot;><input type=&quot;radio&quot; name=&quot;grp1&quot;>Option2</span><br>
<span id=&quot;2&quot; onClick=&quot;ckrad(this.id)&quot;><input type=&quot;radio&quot; name=&quot;grp1&quot;>Option3</span></form>
</body></html>



Great Javascript Resource:
 
Thanks, it's working fine!!!!!!!!
 
There is an easier way...

As far as I know, the LABEL tag is still in the W3C specs. It certainly works in IE 6, NN 7, Opera 7, and Mozilla 1.5.

You simply have a LABEL tag with the label text inside it, with a FOR attribute containing the ID of the associated input element.

Here's the code:

Code:
<HTML>
<HEAD>
</HEAD>
<BODY>

<FORM>
	Do you like Tek-Tips?
	<BR>

	<INPUT ID=&quot;yesno_yes&quot; TYPE=&quot;radio&quot; NAME=&quot;yesno&quot; VALUE=&quot;yes&quot; CHECKED>
	<LABEL FOR=&quot;yesno_yes&quot;>Yes</LABEL>

	<BR>

	<INPUT ID=&quot;yesno_no&quot; TYPE=&quot;radio&quot; NAME=&quot;yesno&quot; VALUE=&quot;no&quot;>
	<LABEL FOR=&quot;yesno_no&quot;>No</LABEL>
	
		
	
</FORM>

</BODY>
</HTML>

Hope this helps!

Dan
 
Thanks, it works really good and it's more simple.
 
Thanks!

I had this feeling there was a way to do it, I just didn't know it, so flicked through the trust O'Reilly DHTML book, and read up on it ;o)

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top