Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check a checkbox after hitting a reset button

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have two radio buttons:


This below is the reset button in the form:


What I want to do when user clicks the "Reset selection" button, I want to check the historicalTimeDataOption checkbox. But it doesn't seem to work. The checkbox is unchecked after I press the reset button.
Here's my function:


Thanks in advance for all the help. Don't hesitate to let me know if I can provide with more information.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do :


But why do you want to do it like this? From your requirement I think you want that 2nd radio button to be selected by default. For that just use the "checked" attribute. Eg:


Regards
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You're changing an attribute, not setting a variable.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg + Himanshu,

Thanks for your suggestions. I'll give it a try and let you know how it goes.


What is the difference between these two suggestions?
And also how is it different from the one that I do?

Thanks.
 
Rajkishore Pujari
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the way you have already coded should work fine as it is, unless some other functionality in your script causing it not to work. There is no difference how you access the radio button using array or id.

You can access a radio object by searching through the elements[] array of the form, or by using document.getElementById().

Here are some links that might help you.

http://www.w3schools.com/htmldom/dom_obj_radio.asp

http://www.w3schools.com/js/tryit.asp?filename=try_dom_radio_checked
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try the two suggestions and they don't work either.



Below is my code:
 
Rajkishore Pujari
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I guess I didn't look at the code keenly before. You were using input type "reset". So according my understanding it resets the page to look like when its loaded. And your onclick function will be executed before it actually does the reset, that means your changes are lost. So either you use input type "button" to get your changes or have the checked="checked" attribute in the input radio for default checked
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to use a setTimeout to cause it to happen after the reset fires. Right now you are clicking it, than the reset action happens, which resets it back.

Eric
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say just 2 things:

1. edit your function as -
and the call as -
<input type="reset" name="resetBtn" id="resetBtn" value="Reset selection"
onclick="return resetSpeedResultTable(this.form);" />
There is another way to do the same thing, first try this.

2. This all can be done by the power of just "1 word" if you tell : "My requirement is to have that radio button selected by default". Do you want that radio to be selected when the page loads? PLEASE do answer this.

Regards
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu, thanks. I'll try your suggestion.
Regarding this: "My requirement is to have that radio button selected by default", the answer is no.

Eric, how can I use setTimeout()? can you please explain more?

Thanks guys.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change your js function like this



and change your input type reset to this

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That last post will not reset the form since it is cancelling the click.

Eric, how can I use setTimeout()? can you please explain more?


Did you look up setTimeout in your favorite search engine?

Eric
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik, Himanshu: Eric is right, the reset button is cancelled.

I add this one line, looks good now:

Thanks for the help guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic