How to get the previous selected item and the current selected item from the drop-down list using Javascript?

advertisements

How to get previous selected item and current selected item from dropdownlist using Javascript?I think, use couple of hidden fields and store previous and current value in them. When the dropdownlist value is changed, copy current hidden field value to prev. hidden field value and then replace current hidden field value with selected drop down value.But i am new to jquery.So i dont know how to work above condition.Any one know Jquery means answer me.


For current selected item this should work

var yourSelect = document.getElementById('id');
alert(yourSelect.options[yourSelect.selectedIndex].value)

For previous

<select name="test">
    <option value="test1">test1</option>
    <option value="test2">test2</option>
    <option value="test3">test3</option>
    <option value="test4">test4</option>
</select>

(function () {
    var previous;

    $("select[name=test]").focus(function () {
        // Store the current value on focus, before it changes
        previous = this.value;
    }).change(function() {
        // Do soomething with the previous value after the change
        document.getElementById("log").innerHTML = "<b>Previous: </b>"+previous;

        previous = this.value;
    });
})();