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


Groups > comp.lang.javascript > #7496

Re: Syntax for resetting radio button to initial value?

From Matt McDonald <matt@fortybelow.ca>
Newsgroups comp.lang.javascript
Subject Re: Syntax for resetting radio button to initial value?
Date 2011-10-16 16:49 -0600
Organization A noiseless patient Spider
Message-ID <j7fn0u$bhp$1@dont-email.me> (permalink)
References <j7fhjv$btu$1@dont-email.me>

Show all headers | View raw


On 10/16/2011 3:16 PM, Garry Jones wrote:
> The bit I am stuck with is
>
> if (elements[i].checked) {
> elements[i].checked = false;
> }
>
> This unclicks the radio buttons. However on the actual form its more
> like this...
>
> O No thanks
> O 1
> O 2
> O 3
>
> Initially the form loads with "No thanks" already checked. If the user
> has now clicked 1, 2 or 3 and uses my reset button to call this function
> I want to use this to set all radio buttons to [0] (ie to check "No
> Thanks" or whatever the first value [0] is.

You'll want to look into HTMLInputElement.defaultChecked[0]. It stores 
the value of the "checked" attribute.

Furthermore, you may want to look into HTMLInputElement.defaultValue[1]. 
It functions like "defaultChecked", except it applies to the "value" 
attribute.

I've made a demo to help you understand:

http://jsbin.com/omomog/edit#source

I do have to question (as Jukka has already done) why you're avoiding 
HTMLFormElement.reset (and functionality like it). It will reset form 
elements back to their default state. Are you trying to make a hybrid 
solution that both clears certain elements, and resets others? Some 
clarity, along with the form markup itself would be welcome.

[0]: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-20509171
[1]: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26091157


-- 
Matt McDonald: Web/Flash Developer; Edmonton, Alberta, Canada

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


Thread

Syntax for resetting radio button to initial value? "Garry Jones" <garry@garryjones.se> - 2011-10-16 23:16 +0200
  Re: Syntax for resetting radio button to initial value? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-10-17 00:46 +0300
    Re: Syntax for resetting radio button to initial value? dhtml <dhtmlkitchen@gmail.com> - 2011-10-16 22:56 -0700
  Re: Syntax for resetting radio button to initial value? Matt McDonald <matt@fortybelow.ca> - 2011-10-16 16:49 -0600
    Re: Syntax for resetting radio button to initial value? dhtml <dhtmlkitchen@gmail.com> - 2011-10-16 22:57 -0700
    Re: Syntax for resetting radio button to initial value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-17 09:56 +0200
  Re: Syntax for resetting radio button to initial value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-17 09:51 +0200
  Re: Syntax for resetting radio button to initial value? Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-17 10:56 +0000
    Re: Syntax for resetting radio button to initial value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-17 14:32 +0200
  Re: Syntax for resetting radio button to initial value? Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-17 11:16 +0000
    Re: Syntax for resetting radio button to initial value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-17 14:45 +0200
    Re: Syntax for resetting radio button to initial value? "Garry Jones" <garry@garryjones.se> - 2011-10-17 20:31 +0200
      Re: Syntax for resetting radio button to initial value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-18 10:45 +0200
  Re: Syntax for resetting radio button to initial value? Dr J R Stockton <reply1142@merlyn.demon.co.uk> - 2011-10-18 20:50 +0100

csiph-web