472,780 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 19385
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt | last post by:
I want to write a generic clearForm function to clear the form when the user click CLEAR button. Here's my attempts, because I want to take care all html controls. I think I need to test if the...
2
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially, the form allows a user to enter as many names as...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
2
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put...
24
by: John Gabriel | last post by:
CreateDialogIndirect() fires my Radio button click function without the radio button having being clicked. I am using VC++ 2003.NET. I believe this is yet another Microsoft bug. Have checked dialog...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see...
5
by: mad.scientist.jr | last post by:
According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: I tried writing a reusable function to return the value, see code at:...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.