469,903 Members | 2,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Function to unselect radio buttons

I use window.document.form1.myCheckbox.checked=false;
to uncheck (reset) several checkboxes. What is an equivalent for resetting
a set of interrelated radio boxes?

--
Ed Jay (remove M to respond by email)
Nov 24 '05 #1
4 19267
Ed Jay wrote:
I use window.document.form1.myCheckbox.checked=false;
to uncheck (reset) several checkboxes. What is an equivalent for resetting
a set of interrelated radio boxes?


Not quite sure what you mean by 'a set of interrelated radio boxes'.

I'll guess that you mean a set of radio buttons that all share the same
name ('myCheckbox' in your example) and that 'resetting' means return
them to their default state when the page loaded.

The trivial answer is if they are in a form, reset the form. If they
aren't, or you want to reset just the set of buttons, loop through them
and set their checked property to their 'defaultChecked' property:
var cBoxes = document.form1.myCheckbox;
for(var i=0, n=cBoxes.length; i<n; ++i){
cBoxes[i].checked = cBoxes[i].defaultChecked;
}
As a matter of interest, opinion is divided on whether one radio button
should always be checked - the relevant part of the HTML spec says that:

"...authors should ensure that in each set of radio buttons that
one is initially 'on'."

<URL: http://www.w3.org/TR/html4/interact/forms.html#radio >
--
Rob
Nov 24 '05 #2
RobG <rg***@iinet.net.au> wrote:
Ed Jay wrote:
I use window.document.form1.myCheckbox.checked=false;
to uncheck (reset) several checkboxes. What is an equivalent for resetting
a set of interrelated radio boxes?
Not quite sure what you mean by 'a set of interrelated radio boxes'.

I'll guess that you mean a set of radio buttons that all share the same
name ('myCheckbox' in your example) and that 'resetting' means return
them to their default state when the page loaded.


Yes.
The trivial answer is if they are in a form, reset the form. If they
aren't, or you want to reset just the set of buttons, loop through them
and set their checked property to their 'defaultChecked' property:
The form is several pages long, with each page loaded from a Perl script.
I'm passing variables from page to page by submitting them as hidden input
form elements.
var cBoxes = document.form1.myCheckbox;
for(var i=0, n=cBoxes.length; i<n; ++i){
cBoxes[i].checked = cBoxes[i].defaultChecked;
} Does document.form1.reset() reset the hidden input elements as well?
As a matter of interest, opinion is divided on whether one radio button
should always be checked - the relevant part of the HTML spec says that:

"...authors should ensure that in each set of radio buttons that
one is initially 'on'."

<URL: http://www.w3.org/TR/html4/interact/forms.html#radio >


Point well taken. How about a reset button as default. That's what I just
did. :-)

Looping through selected elements appears to be the best way to go.

--
Ed Jay (remove M to respond by email)
Nov 24 '05 #3
Ed Jay wrote:

[...]
Does document.form1.reset() reset the hidden input elements as well?


In IE, yes. In Firefox, no. calling the form's reset method is the
same as clicking on a rest button in the form (as far as I can tell).

My reading of the spec is that the values should be reset to their
initial value, so Firefox is out of step here. However if you are not
modifying the hidden inputs in the current page prior to posting, that
shouldn't be problem.

If you are using script to put values from the search string into the
form, you are in trouble as IE will clear the hidden inputs if reset is
clicked. You'll need to get the URL and store it, then have your own
reset button that resets visible controls but not hidden ones.

[...]

--
Rob
Nov 24 '05 #4
On 2005-11-24, Ed Jay <ed***@aes-intl.com> wrote:

The form is several pages long, with each page loaded from a Perl script.
The form is no larger than what is loaded into the broowser at any one time.
I know of no broweser that interacts directly with perl.
I'm passing variables from page to page by submitting them as hidden input
form elements.


It sounds to me like you are passing variables from form to form

if the hidden elements have been modified (for example by javascript)
then they will be reset to the values the server served them with.

Bye.
Jasen
Nov 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Oscar Monteiro | last post: by
2 posts views Thread by dpazza | last post: by
4 posts views Thread by Blasting Cap | last post: by
5 posts views Thread by mad.scientist.jr | last post: by
53 posts views Thread by souporpower | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.