Click here to Skip to main content
Click here to Skip to main content

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.

Comments and Discussions

 
QuestionReset form with 1 line of code PinprofessionalKartik Goyal21-May-14 18:34 
QuestionSimple Alternate Solution PinprofessionalSunasara Imdadhusen21-May-14 3:30 
AnswerRe: Simple Alternate Solution PinprofessionalJagbir Saini21-May-14 5:11 
QuestionWhy reinventing the wheel? PinmemberAiscrim20-May-14 2:37 
AnswerRe: Why reinventing the wheel? PinprofessionalJagbir Saini20-May-14 4:21 
QuestionHow to reset a specific form PinmemberTridip Bhattacharjee14-May-14 21:50 
AnswerRe: How to reset a specific form PinprofessionalJagbir Saini15-May-14 1:16 
QuestionNice Article Dude.... PinmemberMember 950797814-May-14 20:32 
AnswerRe: Nice Article Dude.... PinprofessionalJagbir Saini14-May-14 23:11 
GeneralMy vote of 3 PinprofessionalNitij14-May-14 6:38 
GeneralRe: My vote of 3 PinprofessionalJagbir Saini14-May-14 6:42 
GeneralMy vote of 3 PinmemberIvandro Ismael14-May-14 4:06 
GeneralRe: My vote of 3 PinprofessionalJagbir Saini14-May-14 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.141015.1 | Last Updated 20 May 2014
Article Copyright 2014 by Jagbir Saini
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid