Emptying a Set of Radio Buttons


for (var i=0; i<f.elements["colors"].length; i++) {   f.elements["colors"][i].checked = false; } 

One commonly known disadvantage of radio buttons is that after a button in a group is selected, it is impossible to remove the selection from the groupat least one button is always selected from then on. With a little bit of JavaScript, this effect can be mitigated by offering a JavaScript button or link that removes the radio button selection. The preceding code iterates over the radio button group and sets the checked property of all elements to false (complete code in the file emptyradio.html).

Tip

This issue can also be solved by HTML alone: Just offer a radio button (selected by default) that stands for "no selection," in the following fashion:

<input type="radio" name="answer" value="Y" />yes <input type="radio" name="answer" value="N" />no <input type="radio" name="answer" value=""   checked="checked" />no answer 


Of course, you would then also have to change any validation code.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net