Click here to Skip to main content
15,887,349 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Reset all Fields using jQuery

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
21 May 2014CPOL 17.5K   4   4
This is an alternative for "Reset all Fields using jQuery"

Introduction

Reseting the form to it’s initial values is one of those things you do often.

Doing this task with jQuery is really easy:
XML
<form id="FormID" name= "FormID" action="#" method="get">
 your input controls here
</form>


//Using single line you can clear all the inputs from the form.
$("#mybutton").click(function(){
   $('#FormID').each (function(){
      this.reset();
   });
});
//OR you may using this code
//reset form 
$("#mybutton").click(function(){
    $("#FormID").find('input:text, input:password, input:file, select, textarea').val('');
    $("#FormID").find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
});

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


AWARDS:

  1. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-May-14 19:16
Humayun Kabir Mamun21-May-14 19:16 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen21-May-14 19:20
professionalSunasara Imdadhusen21-May-14 19:20 
SuggestionNice effort ! Pin
Jagbir Saini21-May-14 5:16
professionalJagbir Saini21-May-14 5:16 
yes, nice alternative. Your 2nd option is same as mine but quite short one and it will work absolutely fine.Thumbs Up | :thumbsup:
But there can be a problem with this.reset() which I have mentioned below.

There is a potential issue when resetting form values in an ASP.NET WebForms application using this approach. The issue arises because the reset button does not clear out textboxes and return drop-down lists to their first value, but instead returns the forms fields to their initial values. That is, if you have a page with a textbox whose markup contains a value attribute, like:

<input type="text" value="Hello!"   />


And then a user changes the textbox’s text to “Goodbye!” and then clicks the reset button, the textbox does not go blank – rather, it reverts to “Hello!” In an ASP.NET WebForm application anytime there is a postback on the page – such as if there is a DropDownList whose AutoPostBack property is set to True – the page’s markup is re-rendered and the text values and DropDownList selections made by the user are remembered because the re-rendered markup includes the user-entered values. Long story short, if you use the reset button approach described above and have a form where there are postbacks going on, if a user enters values into textboxes (or drop-downs), then does a postback, then clicks the reset button, the form fields will be reset to their values immediately after the postback and not to empty textboxes.

So, jQuery code can be implemented regardless of postbacks.
GeneralRe: Nice effort ! Pin
Sunasara Imdadhusen21-May-14 19:21
professionalSunasara Imdadhusen21-May-14 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.