Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep selected option (form/select) after refresh [duplicate]

Possible Duplicate:
Html select option lost data after submit

I have a select menu that should keep the selected option after the page refresh. This is the example:

<select id="form_frame" name="frame" onchange="getData(this);"/>
   <option value="data1" selected="selected">Data 1</option>
   <option value="data2">Data 2</option>
</select>

Function getData just pull info to the user.

I'm using Smarty/php for the dynamic content.

Open to advice, thanks!

like image 363
Diego Sarmiento Avatar asked Feb 19 '26 03:02

Diego Sarmiento


1 Answers

How it's done with local storage :

$(function() {
    if (localStorage.getItem('form_frame')) {
        $("#form_frame option").eq(localStorage.getItem('form_frame')).prop('selected', true);
    }

    $("#form_frame").on('change', function() {
        localStorage.setItem('form_frame', $('option:selected', this).index());
    });
});

FIDDLE

like image 98
adeneo Avatar answered Feb 21 '26 17:02

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!