1

I need to reset radio buttons and checkboxes within a form via code when needed.

I am using an indirect approach, where I have placed a reset button and hidden it using CSS.

<form class="form-horizontal form form-existing" role="form" id="form-existing" action="">
  <input type="reset" name="reset-form-new" id="resetFormNew" value="" />
<div class="radio">
  <label>
    <input type="radio" name="new-turnover" id="new-turnover" value="€1 000 - €5 000"/> €1 000 - €5 000
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="new-turnover" id="new-turnover" value="€50 000 - €20 0000"/> €50 000 - €20 0000
  </label>
</div>
<div class="checkbox">  
  <label>  
    <input type="checkbox" name="new-preference" value="Mobile Phones"> Mobile phones  
  </label>
</div>  
<div class="checkbox">  
  <label>  
    <input type="checkbox" name="new-preference" value="Accessories"> Accessories  
  </label>
</div>

Then I try to click it like $("#resetFormNew").trigger("click");

This resets all the text boxes but not the radio buttons and check boxes.

Where am I going wrong?

4
  • Because there no default selection for radio and check boxes in your html code. Add attribute checked for default selection Commented Sep 23, 2014 at 9:00
  • You code is working, check this jsfiddle.net/79wdswL7/9 Commented Sep 23, 2014 at 9:08
  • are there any console errors on browser? Commented Sep 23, 2014 at 9:12
  • can you reproduce the same on jsfiddle? Commented Sep 23, 2014 at 9:33

1 Answer 1

2

A form has its own reset method you can call. So rather than adding steps by hiding a reset button, you can call something like:

$("#form")[0].reset();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.