coder
coder

Reputation: 6235

Is there a way to reset a select to the option that was selected on load using jquery

Is there a way to reset a select to the option that was selected on load using jquery without having to create a custom cache of the initial value?

Upvotes: 0

Views: 102

Answers (2)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Edit: As per your comment, updated the code to create custom attr to the select element and use them for reset. See DEMO here.

//cache all the initial values of the drop down in a custom attr.
$('select').each (function (index) {
    $(this).attr('initial-value', $(this).val());
});

//reset is the button that will be used to reset the value of 
//all the drop down to its initial value.
$('#reset').click (function () {
    $('select').each (function (index) {
        $(this).val($(this).attr('initial-value'));
    });
});

Below is the old post which uses a simple javascript variable to save the initial values,

You can cache the initial value in a simple cache and then use them to reset.

See DEMO here

Upvotes: 1

DamirR
DamirR

Reputation: 1705

There is form.reset method but it will reset other elements of form as well.

Otherwise put some class on option to identify that option as default.

Upvotes: 0

Related Questions