Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12339

Re: Radio Buttons

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: Radio Buttons
Date 2012-03-02 08:49 +0200
Organization A noiseless patient Spider
Message-ID <jipqhb$5f5$1@dont-email.me> (permalink)
References <jipbof$ptp$1@speranza.aioe.org>

Show all headers | View raw


2012-03-02 4:38, emf wrote:

> Example of radio buttons:
>
> <form name="test">
> NBC: <input type="radio" name="station" onClick="theone=0"><br>
> CBS: <input type="radio" name="station" onClick="theone=1"><br>
> ABC: <input type="radio" name="station" onClick="theone=2"><br>
> </form>
>
> Is there a way for a radio button to get checked not only when you click
> directly on it, but also when you click on its legend (in the above
> example NBC: etc.), so it won't be difficult to aim the mouse?

Yes, and you do this in HTML, not JavaScript, by wrapping the text in a 
<label> element that has a for="..." attribute, with a value matching 
the id="..." attribute in the <input> element. Some old browsers do not 
have the functionality of binding texts with fields that way, but I 
would not bother trying to simulate it with JavaScript. (You could add 
an event handler that reacts to clicking on the text, but then you would 
have the problem that the button setting would be toggled twice, on 
browsers that support <label>.)

Moreover, instead of the onclick handlers, just use a value="..." 
attribute. If you need to retrieve the radio button setting in 
JavaScript, you need to use a loop (or use jQuery or some other 
library), but it's still better than the onclick approach.

Example:

<input type="radio" name="station" value="0" id="nbc"><label 
for="nbc">NBC</label>

It is normal to put the label after the radio button, not before it. 
This is so common that deviating from the practice could confuse people.

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Radio Buttons emf <emfril@gmail.com> - 2012-03-01 21:38 -0500
  Re: Radio Buttons "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-03-02 08:49 +0200
    Re: Radio Buttons Swifty <steve.j.swift@gmail.com> - 2012-03-02 09:01 +0000
    Re: Radio Buttons "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-02 14:13 +0000
  Re: Radio Buttons BootNic <bootnic.bounce@gmail.com> - 2012-03-02 01:54 -0500
    Re: Radio Buttons emf <emfril@gmail.com> - 2012-03-02 23:24 -0500
      Re: Radio Buttons "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-03 10:35 +0000
  Re: Radio Buttons Dr J R Stockton <reply1209@merlyn.demon.co.uk> - 2012-03-03 20:26 +0000
    Re: Radio Buttons SAM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> - 2012-03-06 17:48 +0100
      Re: Radio Buttons "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-06 19:06 +0000
      Re: Radio Buttons Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-07 19:31 +0000

csiph-web