Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnChange - get previous value? [duplicate]

Is it possible to get the previous value from the OnChange event from a drop down? I need to see the previous selected value, so that I can make a decision on what I should display.

At the moment, I get the new value:

var id = $('.cmbType').val();

But would like to know what it was before the user selected this value.

like image 835
Craig Avatar asked Mar 03 '26 21:03

Craig


1 Answers

You have to store the previous value yourself:

// store initial value
var initialValue = $('.cmbType').val();
$('.cmbType').data('previousValue', initialValue);

// change handler
$('.cmbType').change(function(e) {

    var previousValue = $(this).data('previousValue');

    // make decision
    alert(previousValue);

    // store previousValue
    $(this).data('previousValue', $(this).val());
});
like image 103
strictlyk3v Avatar answered Mar 06 '26 10:03

strictlyk3v



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!