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

Reset all Fields using jQuery

Rate me:
Please Sign up or sign in to vote.
4.85/5 (11 votes)
19 May 2014CPOL 52.4K   4   16   16
Cleared all the fields in the asp.net form using jQuery on single click

Introduction

This is a common requirement while developing a web form which consists of various Input controls like Textbox, Radio Button, Checkbox, DropdownList, File Upload etc. We also need to have a button which clears the value of all the fields. Generally we named this Button as Reset.

Background

Every developers have their different approach of Resetting all the controls on a single click. I am explaining a simple jQuery function which is called on the click of button and reset values of all the controls used in the web page.

Using the code

  1. Take a reference of latest jQuery in your source. I am using jquery-1.10.2.js in my example.
  2. In the form tag of your Web form add your controls which you would like to use in the page.
  3. Simply Copy and Paste the below code in the script tag :
JavaScript
<script type="text/javascript">
       function resetFields(form) {
           $(':input', form).each(function() {
               var type = this.type;
               var tag = this.tagName.toLowerCase(); // normalize case
               // to reset the value attr of text inputs,
               // password inputs, fileUpload and textareas
               if (type == 'text' || type == 'password' || tag == 'textarea' || type=='file')
                   this.value = "";
               // checkboxes and radios need to have their checked state cleared
               else if (type == 'checkbox' || type == 'radio')
                   this.checked = false;
               // select elements need to have their 'selectedIndex' property set to -1
               // (this works for both single and multiple select elements)
               else if (tag == 'select')
                   this.selectedIndex = 0;
           });
       }

   </script>
  1. And then simply call this function on button OnClientClick event.
ASP.NET
<asp:Button ID="Button1" runat="server" Text="Reset" OnClientClick="resetFields(form1);" />  
  1. Press f5 and check.

License

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


Written By
Software Developer
India India
A .NET Developer with 4 years commercial experience concentrating primarily with C#.Net, Asp.Net, Ado.net, HTML, JQuery, CSS and SQL Server with n-tier architecture and having knowledge of MVC design pattern and Entity Framework. Within these main areas has developed many internet based applications.

Comments and Discussions

 
Questionnot applicable for multipleselect Pin
Member 1399579422-Nov-18 1:33
Member 1399579422-Nov-18 1:33 
QuestionReset form with 1 line of code Pin
Kartik Goyal21-May-14 18:34
professionalKartik Goyal21-May-14 18:34 
QuestionSimple Alternate Solution Pin
Sunasara Imdadhusen21-May-14 3:30
professionalSunasara Imdadhusen21-May-14 3:30 
AnswerRe: Simple Alternate Solution Pin
Jagbir Saini21-May-14 5:11
professionalJagbir Saini21-May-14 5:11 
QuestionWhy reinventing the wheel? Pin
Aiscrim20-May-14 2:37
Aiscrim20-May-14 2:37 
AnswerRe: Why reinventing the wheel? Pin
Jagbir Saini20-May-14 4:21
professionalJagbir Saini20-May-14 4:21 
AnswerRe: Why reinventing the wheel? Pin
Member 1399579422-Nov-18 1:10
Member 1399579422-Nov-18 1:10 
GeneralRe: Why reinventing the wheel? Pin
Aiscrim22-Nov-18 5:12
Aiscrim22-Nov-18 5:12 
QuestionHow to reset a specific form Pin
Tridip Bhattacharjee14-May-14 21:50
professionalTridip Bhattacharjee14-May-14 21:50 
AnswerRe: How to reset a specific form Pin
Jagbir Saini15-May-14 1:16
professionalJagbir Saini15-May-14 1:16 
QuestionNice Article Dude.... Pin
Jeevan Malepati14-May-14 20:32
Jeevan Malepati14-May-14 20:32 
AnswerRe: Nice Article Dude.... Pin
Jagbir Saini14-May-14 23:11
professionalJagbir Saini14-May-14 23:11 
GeneralMy vote of 3 Pin
Nitij14-May-14 6:38
professionalNitij14-May-14 6:38 
GeneralRe: My vote of 3 Pin
Jagbir Saini14-May-14 6:42
professionalJagbir Saini14-May-14 6:42 
GeneralMy vote of 3 Pin
Ivandro Ismael14-May-14 4:06
Ivandro Ismael14-May-14 4:06 
GeneralRe: My vote of 3 Pin
Jagbir Saini14-May-14 4:08
professionalJagbir Saini14-May-14 4:08 

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.