User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 425,820 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,004 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2640 | Replies: 8 | Solved
Reply
Join Date: Mar 2008
Posts: 12
Reputation: farahphp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
farahphp farahphp is offline Offline
Newbie Poster

click label to select radio button

  #1  
May 16th, 2008
Hi All
i want to develop a code such that when i click on the text next to the radio button the radio button should be selected and even if the button is clicked
how can i achieve this
any help would b appriciated
thanks in advance


<html>
	<head>
		<script language="Javascript">

     </script>
</head>
<body>
<table>
<tr>
      		<td width="4%" valign="top" align="left"><font face="Times New Roman" size="3"
          color="#000000">1.</font></td>
      		<td width="30%" valign="top" align="left" nowrap><font face="Times New Roman" size="3"
          color="#000000">Select an option</font></td>
      		<td width="73%" valign="top" align="left">
	 			<input type="radio" name="grp" value="Yes" tabindex="1">Yes
	  			<input type="radio" name="grp" value="No" tabindex="1">No</td>
    		</tr>
</table>
</body>
</html>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: o0DarkEvil0o is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
o0DarkEvil0o's Avatar
o0DarkEvil0o o0DarkEvil0o is offline Offline
Newbie Poster

Re: click label to select radio button

  #2  
May 16th, 2008
Code bellow should help you, good luck!

<html>
	<head>
		<script language="Javascript">

     </script>
<script>
function selectrd(id)
{
	var opt=(id==1)?"yes":"no";
	var tg=document.getElementById("rd" + opt);
	tg.checked=true;
}
</script>
</head>
<body>
<table>
<tr>
      		<td width="4%" valign="top" align="left"><font face="Times New Roman" size="3"
          color="#000000">1.</font></td>
      		<td width="30%" valign="top" align="left" nowrap><font face="Times New Roman" size="3"
          color="#000000">Select an option</font></td>
      		<td width="73%" valign="top" align="left">
	 			<input id="rdyes" type="radio" name="grp" value="Yes" tabindex="1">Yes
	  			<input id="rdno" type="radio" name="grp" value="No" tabindex="1">No</td>
    		</tr>
</table>
<label onclick="selectrd(1)">Select Yes</label> | 
<label onclick="selectrd(2)">Select No</label> 

</body>
</html>
http://boy.us.com/ - Multi Utilities Site
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,855
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: click label to select radio button

  #3  
May 18th, 2008
> i want to develop a code such that when i click on the text next to the radio button the radio
> button should be selected and even if the button is clicked
You don't need Javascript for this; use the HTML label tag.
<p>Try clicking on the text labels:</p>
<form name="input" action="">
<input type="radio" name="sex" id="male" />
<label for="male">Male</label>
<br />
<input type="radio" name="sex" id="female" />
<label for="female">Female</label>
</form>
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Aug 2008
Posts: 3
Reputation: mnjbadc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
mnjbadc mnjbadc is offline Offline
Newbie Poster

Re: click label to select radio button

  #4  
Aug 11th, 2008
There is a potential problem relying on the html label solution above: I am trying to set up a list of buttons using javascript to highlight the selected option.

<span onClick = "jfsw(2,la,10);">
<input type="radio" name="cruts21t" id="19111920" value="19111920" />
<label class="radio" id="cruts21tb" for="19111920">1911-1920</label>
</span>

In the above, the javascript function jfsw highlights the label when the input or label is clicked on. The problem is that firefox 2.0.0.13 leaves a small space between the input symbol and the label. Clicking on this space activates the javascript but does not select the corresponding radio button. Internet Explorer appears not to have this problem.

Can the format of the label be made to change when the button is checked without using javascript?
Last edited by Tekmaven : Aug 12th, 2008 at 4:56 am. Reason: Code tags
Reply With Quote  
Join Date: Aug 2008
Posts: 374
Reputation: langsor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 34
langsor langsor is offline Offline
Posting Whiz

Re: click label to select radio button

  #5  
Aug 11th, 2008
This is how I would do it
Originally Posted by ~s.o.s~ View Post
<p>Try clicking on the text labels:</p>
<form name="input" action="">
<input type="radio" name="sex" id="male" />
<label for="male">Male</label>
<br />
<input type="radio" name="sex" id="female" />
<label for="female">Female</label>
</form>
Reply With Quote  
Join Date: Aug 2008
Posts: 374
Reputation: langsor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 34
langsor langsor is offline Offline
Posting Whiz

Re: click label to select radio button

  #6  
Aug 11th, 2008
Originally Posted by mnjbadc View Post
<span onClick = "jfsw(2,la,10);">
<input type="radio" name="cruts21t" id="19111920" value="19111920" />
<label class="radio" id="cruts21tb" for="19111920">1911-1920</label>
</span>


In the above, the javascript function jfsw highlights the label when the input or label is clicked on. The problem is that firefox 2.0.0.13 leaves a small space between the input symbol and the label. Clicking on this space activates the javascript but does not select the corresponding radio button. Internet Explorer appears not to have this problem.

Can the format of the label be made to change when the button is checked without using javascript?

<span onClick = "jfsw(2,la,10);">
<input type="radio" name="cruts21t" id="19111920" value="19111920" /><label class="radio" id="cruts21tb" for="19111920">1911-1920</label>
</span>

Try that ... the newline is interpreted as a space-character between the radio and the label

...
Reply With Quote  
Join Date: Aug 2008
Posts: 3
Reputation: mnjbadc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
mnjbadc mnjbadc is offline Offline
Newbie Poster

Re: click label to select radio button

  #7  
Aug 12th, 2008
Thanks Langsor, that reduces the gap, but does not eliminate it. With Firefox rendering, there is also a small area beneath the input button, but inside the enclosing span, which does not change the selection.
Reply With Quote  
Join Date: Feb 2008
Location: Gurgaon India
Posts: 181
Reputation: nikesh.yadav is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 14
nikesh.yadav's Avatar
nikesh.yadav nikesh.yadav is offline Offline
Junior Poster

Re: click label to select radio button

  #8  
Aug 12th, 2008
use this only a little change

add lable

<label><input type="radio" name="grp" value="Yes" tabindex="1">Yes</label>
Help as an alias

I think programming is great................
Go To My Home Page
Reply With Quote  
Join Date: Aug 2008
Posts: 3
Reputation: mnjbadc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
mnjbadc mnjbadc is offline Offline
Newbie Poster

Re: click label to select radio button

  #9  
Aug 12th, 2008
Thanks Nikesh, that solves the problem!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC