user475685
user475685

Reputation: 8319

how do i get the previous selected drop down item using javascript?

i have a drop down menu, on clicking any item from the list i want to get the previously selected item. Is it possible using javascript?

Upvotes: 1

Views: 4856

Answers (2)

mplungjan
mplungjan

Reputation: 178011

You mean

<script>
var selHistory =[];
var sel; 
window.onload=function() {
  sel = document.getElementById('selID');
  sel.onchange=function() {
    selHistory[selHistory.length]=sel.selectedIndex;
  }
  sel.onchange(); // save the current option
}
function getPrev() {
  return (selHistory.length < 1) ? "no previous":sel.options[selHistory[selHistory.length-2]].value
}
</script>

or perhaps

<select onChange="var prevSel=(this.selectedIndex>0) ? this.options[this.selectedIndex-1].value:'nothing previous'">

Upvotes: 2

Timur Sadykov
Timur Sadykov

Reputation: 11377

You can just save selected item into some var and read from it when next item will be selected.

Upvotes: 0

Related Questions