The thing i want to achieve is whenever the <select>
dropdown is changed i want the value of the dropdown before change. I am using 1.3.2 version of jquery and using on change event but the value i am getting over there is after change.
<select name="test">
<option value="stack">Stack</option>
<option value="overflow">Overflow</option>
<option value="my">My</option>
<option value="question">Question</option>
</select>
Lets say currently option My is selected now when i change it to stack in the onchange event (ie when i changed it to stack) i want it's previous value ie my expected in this case.
How can this be achieved ?
Edit: In my case i am having multiple select boxes in the same page and want same thing to be applied to all of them. Also all of my select are inserted after page load through ajax.
This is an improvement on @thisisboris answer. It adds a current value to data, so the code can control when a variable set to the current value is changed.
I go for Avi Pinto's solution which uses
jquery.data()
Using focus isn't a valid solution. It works at first time you change the options, but if you stay on that select element, and press key "up" or "down". It won't go through the focus event again.
So the solution should be more looks like the following,
Well, why don't you store the current selected value, and when the selected item is changed you will have the old value stored? (and you can update it again as you wish)
Track the value by hand.
Use following code,I have tested it and its working