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.

3
  • 1
    did you try form.reset() ? Commented Aug 11, 2011 at 9:41
  • As I explained in my description I am passing Div and not form, I know how to reset Form but I need simmilar function for Divs Commented Aug 11, 2011 at 9:46
  • So you want to reset only part of your form and not the whole form? Commented Aug 11, 2011 at 9:49

2 Answers 2

2

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Could you suggest how this function could be updated to support selects and checkboxes
@Tomas: Wrap the inputs in a form. Inputs go in a form (though 18.2.3 doesn't strictly mandate this).
0

Because the value of a radio button is determined not by the "value" attribute, but by the "checked" attribute.

The following code seems to work as expected (http://jsfiddle.net/eVnMA/2/ ):

function reset(form)
{
    $(":input", form).not(":button, :submit, :reset, :hidden").each(function () {
        var type = $(this).attr("type");
        if(type != "radio" && type != "checkbox") this.value = this.defaultValue;
        else this.checked = $(this).data("default_value");
    });

}
$("#cb").data("default_value", null);
$("#btn").click(function(){reset("#aaa");});

Note that it makes no sense to use the defaultValue attribute in case of checkboxes/radio-buttons. But you can keep the desired default value by associating some data with the appropriate element.

However, I still think that the right way to achieve what you want would be to just use normal forms.

5 Comments

Thank you for your example. I will try it right now. Speaking about normal form, I use ASP.NET and two forms in one web page brings many problems.
Sorry but it do not work on checkboxes and selects. Try to uncheck checkbox and click reset, it should be checked again. jsfiddle.net/eVnMA/4
That's because you forgot to modify the default value, $("#cb").data("default_value", null); should become $("#cb").data("default_value", "checked"); in this case.
Do I need to add $("#cb").data("default_value", "checked"); for every checkbox in the form?
Well, if the default value is the same for all checkboxes, you can just do $("#aaa :checkbox").data("default_value", "checked"); (replacing #aaa with the id of the div containing your form, of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.