Reputation: 17
I want to display an alert if I select the "b" option, but before that, I'm in the "a" option, not from the "c" option.
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
Upvotes: 0
Views: 41
Reputation: 50759
You need to create a variable which holds the previous value that you were on. Using this variable you can check whether it was on "a" previously (prev == "a"
).
See working example below:
let prev = $('select').val();
$('select').change(function() {
if(prev == 'a' && $(this).val() == 'b') { // Check if the previous value is 'a' and the current value is 'b'
alert("From a ---> b");
}
prev = $(this).val();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>a</option>
<option>b</option>
<option>c</option>
</select>
Upvotes: 1