Sam Hanson
Sam Hanson

Reputation: 1357

Jquery get the pervious selected value in listbox

I have a list box with 10 elements. now the element 5 is selected. If i change the value to 8 then i need to get both 5 and 8.

i.e I need the currently selected item and previous selected item.

Is there any possibility in Jquery. Please do the needful. Thanks

Upvotes: 1

Views: 759

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337580

Try this:

// on load
$("#mySelect").data("previous-value", $("#mySelect").val());

// on change
$("#mySelect").change(function() {
    var previousValue = $(this).data("previous-value");

    // do things with the previous value

    // update previous value
    $(this).data("previous-value", $(this).val());
});

Example fiddle

Upvotes: 3

Related Questions