17
rated 0 times
[
20]
[
3]
/ answers: 1 / hits: 8024
/ 10 Years ago, wed, may 28, 2014, 12:00:00
How can I set/retrieve the last selected value of a select drop-down with JavaScript? I'm trying to create an onchange function on a select drop-down that that sets the selected option, and then on each page-load, that valued is loaded.
Here is the HTML
<select class=testSelect>
<option value=test1>test1</option>
<option value=test2>test2</option>
<option value=test2>test3</option>
<option value=test2>test4</option>
</select>
I'm having a little trouble with the JavaSCript though.
var select = document.querySelector(.testSelect);
var selectOption = select.options[select.selectedIndex];
var getLast = localStorage.getItem(select, lastSelected);
selectOption = getLast;
select.onchange = function () {
var lastSelected = select.options[select.selectedIndex].value;
localStorage.setItem(select, lastSelected);
}
and here's a fiddle http://jsfiddle.net/5yJNL/1/
More From » html