Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:09:58+00:00 2026-05-24T11:09:58+00:00

I use the code below to set default values for input objects. This works

  • 0

I use the code below to set default values for input objects. This works fine for text inputs but select dropdowns, radio buttons and check boxes are not reset to their default values.

How can I solve this?

function ResetForm(form) {

    $(":input", form).not(":button, :submit, :reset, :hidden").each(function () {
        this.value = this.defaultValue;
    });

}

p.s. I use this method to reset inputs which are in a div node; I pass the div‘s id into this function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:09:59+00:00Added an answer on May 24, 2026 at 11:09 am

    defaultValue exists for radio buttons and checkboxes, but you have misunderstood what the “value” of these elements is. It is not “which” or “whether” is selected/checked, but the underlying value that is part of the form contents if the element is selected/checked… whereas you want the former.

    And defaultValue simply doesn’t exist for select, which doesn’t really have a value, but one or more selected option children.

    You have a couple of options here.


    Option 1: Wrap the inputs in a form

    You said you don’t have a form, but why not make it so that you do? Although the W3C spec doesn’t require that inputs be under a <form /> node, it would make sense.

    Then:

    form.reset()
    

    Or, if your form is a jQuery element set (and it looks like it probably is):

    form[0].reset()
    

    Option 2: Hack it

    If you really insist, you can hack it about, caching initial values using a mix of $.attr and $.prop. Make sure that you’re using jQuery 1.6.1+ though.

    $(function() {
        prepareForm($('#myForm'));
        $('#myButton').click(function() {
           resetForm($('#myForm'));
        });
    });
    
    function prepareForm($form) {
        // Cache initial states
        $('input:checkbox, input:radio', $form).each(function() {
            $(this).prop('initial', $(this).is(':checked'));
        });
        $('select', $form).each(function() {
            $(this).prop('initial', this.selectedIndex);
        });
    }
    
    function resetForm($form) {
        // Reset elements on button press
        $('input:checkbox, input:radio', $form).each(function() {
            $(this).attr('checked', $(this).prop('initial'));
        });
        $('select', $form).each(function() {
            this.selectedIndex = $(this).prop('initial');
        });
    }
    

    Live demo.


    (Note to other contributors: I originally looked at simply resetting the element to the state specified by $.attr, but I guess even 1.6.1+’s $.attr doesn’t work quite how I thought.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been bothering me lately- when I use some code like below to
How to Remove Badge from the tabbar item i had use below code but
Normally I use the below code, but is there a better way? lastOfMonth =
The below code works fine: ListControl lstMyControl; if (SomeVariable == SomeEnum.Value1) { lstMyControl =
I try yo use sigtimedwait according to the code below, I got this message
When I try to use the code below I get a duplicate variable error
I'm trying to use the code below to send messages via System.Net.Mail and am
When I run a Test Project on Visual Studio I use the code below
The code below is what I am trying to use in order to get
I use the below code to write code to query a web method in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.