Search code examples
javascriptjqueryhtmlformsdefault-value

Reset all inputs of a form to default values without reloading


I want to reset all the values of my inputs that I have within a form (textboxs, selects, checkboxs, radiobuttons,...) like if I reload the web, but when I touch a button.

$("#button").click(function () {
     //Here the code that I need   
});

Solution

  • You can reset a form using JavaScript built-in reset() function:

    $("#otra").click(function () {
        $("#yourFormId")[0].reset();
    });
    

    Working demo:

    $("#otra").click(function () {
      $("#yourFormId")[0].reset();
      return false; // prevent submitting
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <form id="yourFormId">
      <input type="checkbox" name="myCB"/>
      <input type="text" name="myTB" value=""/>
      
      <button id="otra">Reset</button>
    </form>