Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > uncheck radio group

Reply
Thread Tools

uncheck radio group

 
 
cerr
Guest
Posts: n/a
 
      11-05-2011
Hi There,

I have a bunch of radio groups on a page and would like to uncheck
them all together on button click.
OnClick I trigger this function:

function uncheck(grpnr){
group="grp"+grpnr;
alert("hello!"+"\r\n"+group+"\r\n");
if (document.getElementByName(group)){
alert("Got it!");
}
for (var i=0; i<document.toppings.group.length; i++) {
document.toppings.group[i].checked = false;
}
}

The radio groups is called "grp4" (with the number incrementing). So i
pass the index number to this function, assemble the group name but
for some reason this doesn't seem to work, any clues what i need to do
differently? I also never get the "Got it!" message...my form tag
looks like this:

<form name="toppings" action="/store/index.php" method="POST">

Thank you for the help!
 
Reply With Quote
 
 
 
 
Elegie
Guest
Posts: n/a
 
      11-06-2011
On 05/11/2011 23:57, cerr wrote :

Hello,

> I have a bunch of radio groups on a page and would like to uncheck
> them all together on button click.


You can then try and use the following function:

function uncheckAll(form) {
var elements = form.elements;
for(var ii=0; ii<elements.length; ii++) {
if (elements[ii].type == "radio") {
elements[ii].checked = false;
}
}
}

.... calling it while passing the form object, e.g. (for an INPUT element
located in the same FORM as the radio controls):

<input type="button" value="Reset All"
onclick="uncheckAll(this.form);">

Note that it is a good practice to always provide a default radio button
per radio button group, so that the user may select this default value
if he/she wants to manually reset the group. In such case, you would
want to uncheck all radio buttons, except for default ones which would
have to be checked back.

> OnClick I trigger this function:
>
> function uncheck(grpnr){
> group="grp"+grpnr;
> alert("hello!"+"\r\n"+group+"\r\n");
> if (document.getElementByName(group)){


------------------------^^-----

You probably mean the "getElementsByName" method, which returns a
collection of all elements bearing the same name?

> alert("Got it!");
> }
> for (var i=0; i<document.toppings.group.length; i++) {


Since "group" is a variable, and that you want to use the value of this
variable, you need to use the square bracket accessor:

document.toppings.elements[group].length

> document.toppings.group[i].checked = false;


and likewise:

document.toppings.elements[group][i]

<snip>

Also, most browsers come up with a javascript console, which report
javascript errors. Try and find yours: it will give you interesting
information about why and where scripts fail. One should always program
with feedback.

Regards,
Elegie.
 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      11-06-2011
On Sat, 05 Nov 2011 15:57:04 -0700, cerr wrote:

> Hi There,
>
> I have a bunch of radio groups on a page and would like to uncheck them
> all together on button click.
> OnClick I trigger this function:
>
> function uncheck(grpnr){
> group="grp"+grpnr;
> alert("hello!"+"\r\n"+group+"\r\n");
> if (document.getElementByName(group)){ alert("Got it!");
> }
> for (var i=0; i<document.toppings.group.length; i++) {
> document.toppings.group[i].checked = false; }
> }
>
> The radio groups is called "grp4" (with the number incrementing). So i
> pass the index number to this function, assemble the group name but for
> some reason this doesn't seem to work, any clues what i need to do
> differently? I also never get the "Got it!" message...my form tag looks
> like this:
>
> <form name="toppings" action="/store/index.php" method="POST">
>
> Thank you for the help!


You need to uncheck the buttons, not the groups.

This might work:

function uncheckAllRadioButtonsInDocument()
{
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++) if (inputs[i].type=="radio") inputs
[i].checked=false;
}

Rgds

Denis McMahon
 
Reply With Quote
 
Swifty
Guest
Posts: n/a
 
      11-06-2011
On Sun, 06 Nov 2011 01:02:25 +0100, Elegie <(E-Mail Removed)>
wrote:

>Note that it is a good practice to always provide a default radio button
>per radio button group, so that the user may select this default value
>if he/she wants to manually reset the group. In such case, you would
>want to uncheck all radio buttons, except for default ones which would
>have to be checked back.


If you don't want such a default button (there are cases) then you can
create it, but set it to be invisible, using CSS.

Then, by checking this one, invisible, button, you automatically
uncheck any other one on the group.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-06-2011
cerr wrote on 05 nov 2011 in comp.lang.javascript:

> I have a bunch of radio groups on a page


Where else?

> and would like to uncheck them all together on button click.


This is a wrong idea IMHO.

A radio-group without any checked is strictly illegal and error-prone.

Better make a hidden radio-button that can be checked by javascript.
And no loop is needed.


<form>
<input type='radio' name='a' value='none'
checked style='display:none;' id='aNone'>
<label><input type='radio' name='a' value='1'> 1</label><br>
<label><input type='radio' name='a' value='2'> 2</label><br>
<label><input type='radio' name='a' value='3'> 3</label><br>
</form>

<button onclick = "document.getElementById('aNone').checked = true;">
Uncheck
</button>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-06-2011
Evertjan. wrote on 06 nov 2011 in comp.lang.javascript:

> cerr wrote on 05 nov 2011 in comp.lang.javascript:
>
>> I have a bunch of radio groups on a page

>
> Where else?
>
>> and would like to uncheck them all together on button click.

>
> This is a wrong idea IMHO.
>
> A radio-group without any checked is strictly illegal and error-prone.
>
> Better make a hidden radio-button that can be checked by javascript.
> And no loop is needed.
>
>
> <form>
> <input type='radio' name='a' value='none'
> checked style='display:none;' id='aNone'>
> <label><input type='radio' name='a' value='1'> 1</label><br>
> <label><input type='radio' name='a' value='2'> 2</label><br>
> <label><input type='radio' name='a' value='3'> 3</label><br>
> </form>
>
> <button onclick = "document.getElementById('aNone').checked = true;">
> Uncheck
> </button>


>> I have a bunch of radio groups on a page


Sorry, I now read "bunch of groups":

<button onclick = "document.getElementById('aNone').checked =
document.getElementById('bNone').checked =
document.getElementById('cNone').checked =
document.getElementById('dNone').checked = true;">
Uncheck groups a,b,c,d
</button>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-06-2011
Evertjan. wrote on 06 nov 2011 in comp.lang.javascript:

> Evertjan. wrote on 06 nov 2011 in comp.lang.javascript:
>
>> cerr wrote on 05 nov 2011 in comp.lang.javascript:
>>
>>> I have a bunch of radio groups on a page

>>
>> Where else?
>>
>>> and would like to uncheck them all together on button click.

>>
>> This is a wrong idea IMHO.
>>
>> A radio-group without any checked is strictly illegal and error-prone.
>>
>> Better make a hidden radio-button that can be checked by javascript.
>> And no loop is needed.
>>
>>
>> <form>
>> <input type='radio' name='a' value='none'
>> checked style='display:none;' id='aNone'>
>> <label><input type='radio' name='a' value='1'> 1</label><br>
>> <label><input type='radio' name='a' value='2'> 2</label><br>
>> <label><input type='radio' name='a' value='3'> 3</label><br>
>> </form>
>>
>> <button onclick = "document.getElementById('aNone').checked = true;">
>> Uncheck
>> </button>

>
>>> I have a bunch of radio groups on a page

>
> Sorry, I now read "bunch of groups":
>
> <button onclick = "document.getElementById('aNone').checked =
> document.getElementById('bNone').checked =
> document.getElementById('cNone').checked =
> document.getElementById('dNone').checked = true;">
> Uncheck groups a,b,c,d
> </button>


Then again, if you have no other radio-inputs with a value of 'none':

<form>
<input type='radio' name='a' value='none'
checked style='display:none;'>
<label><input type='radio' name='a' value='1'> 1</label><br>
<label><input type='radio' name='a' value='2'> 2</label><br>
</form>

<form>
<input type='radio' name='b' value='none'
checked style='display:none;'>
<label><input type='radio' name='b' value='1'> 1</label><br>
<label><input type='radio' name='b' value='2'> 2</label><br>
</form>

<button onclick = "unCheck()">
Uncheck all groups
</button>

<script type='text/javascript'>
function unCheck() {
var allInputs = document.getElementsByTagName('input');
for (var i in allInputs)
if (allInputs[i].type == 'radio' && allInputs[i].value == 'none')
allInputs[i].checked = true;
};
</script>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      11-06-2011
In comp.lang.javascript message <3c81f60b-9264-4083-ba1f-1c87d4636197@q3
5g2000prh.googlegroups.com>, Sat, 5 Nov 2011 15:57:04, cerr
<(E-Mail Removed)> posted:

>I have a bunch of radio groups on a page and would like to uncheck
>them all together on button click.


Add a radiobutton to each group, and check it with your button, which
unchecks the others. IIRC, the new button can be styled to not display.
It could also be marked "none of those".

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swap image with check/uncheck of Radio Button lemat@attglobal.net Javascript 2 01-09-2007 02:29 PM
Swap image with check/uncheck of Radio Button lemat@attglobal.net HTML 0 01-08-2007 01:51 PM
ASP.NET Reset Radio Button in a group of Radio Buttons Scott D Johnson ASP .Net 1 09-27-2006 10:38 PM
how to check/Uncheck a checkbox dynamically in a datagrid in asp.n =?Utf-8?B?U2lsZWVzaA==?= ASP .Net 4 12-02-2004 03:25 PM
Checked radio button doesn't uncheck Ken Loomis Javascript 1 12-31-2003 03:12 AM



Advertisments