Click here to Skip to main content
12,348,946 members (48,433 online)
Click here to Skip to main content
Add your own
alternative version

Stats

24K views
4 downloads
16 bookmarked
Posted

Reset all Fields using jQuery

, 19 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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 :
<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: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)

Share

About the Author

Jagbir Saini
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.

You may also be interested in...

Comments and Discussions

 
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
memberAiscrim20-May-14 2:37 
AnswerRe: Why reinventing the wheel? Pin
Jagbir Saini20-May-14 4:21
professionalJagbir Saini20-May-14 4:21 
QuestionHow to reset a specific form Pin
Tridip Bhattacharjee14-May-14 21:50
memberTridip 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
Member 950797814-May-14 20:32
memberMember 950797814-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
memberIvandro 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 20 May 2014
Article Copyright 2014 by Jagbir Saini
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid