Skip to content Skip to sidebar Skip to footer

Prevent Select Option From Changing Back To Default

I have a page that has 6 options in a drop down menu. I use the below code to make the default select option 'Full Name' $(function(){ $('select option[value='Full

Solution 1:

You will need to store the state of the dropdown either using server or client side technology.

In client side you can use a cookie or html5 storage like local storage to store the selected value and when the page is revisited and there is a stored value then you can select that value instead of the default value

If you are planning to use cookie to store the information, then you can think of a jQuery plugin like this or this

An abstract implementation might look like

$('select').change(function(){
    storeValue('mykey', $(this).val());
})

functionstoreValue(key, value){
    $.cookie(key, value)
}

functiongetValue(key){
    return $.cookie(key);
}

$(function(){
    var val = getValue('mykey') || 'Full Name';
    $('select option[value="' + val + '"]').prop("selected",true);
});

Post a Comment for "Prevent Select Option From Changing Back To Default"