You can store the value in the element's data on a focus
event.
e.g. something like:
$('.selecttag').on('focus', function(){
$(this).data("value", $(this).val());
})
then in the change event you get the data("value")
back.
e.g.
$('.selecttag').on('change', function(){
var orig = $(this).data("value");
var newVal = $(this).val();
// Save the newer value
$(this).data("value", $(this).val());
// Do something with both values!
});
Notes:
- I use jQuery event handlers only above and recommend you do not mix
inline
event handlers with jQuery. This avoid separating the event registration from the event handler and enables the extra event features jQuery provide.
- Using jQuery handlers lets you chain them too:
e.g.
$('.selecttag').on('focus', function(){
$(this).data("value", $(this).val());
}).on('change', function(){
var orig = $(this).data("value");
var newVal = $(this).val();
// Save the newer value
$(this).data("value", $(this).val());
// Do something with both values!
});
The focus is actually redundant after the first time, so you can just do it once, like this:
$('.selecttag').one('focus', function(){
$(this).data("value", $(this).val());
}).on('change', function(){
var orig = $(this).data("value");
var newVal = $(this).val();
// Save the newer value
$(this).data("value", $(this).val());
// Do something with both values!
});