How to show changed input and select fields with jQuery
Here are a few quick easy jquery functions that will add a class of 'changed' to any input or select box if it is changed, and will remove that class if the value is reset to the default.
// inputs
$('form.myForm :input').change(function(){
var defval = $(this)[0].defaultValue;
if ($(this).val()!=defval){
$(this).addClass('changed');
} else {
$(this).removeClass('changed');
}
}).keyup(function(){
$(this).trigger('change');
});
// select boxes
$('form.myForm select').change(function(){
var defval = $(this).find('option[defaultSelected=true]').val();
if ($(this).val()!=defval){
$(this).addClass('changed');
} else {
$(this).removeClass('changed');
}
}).keyup(function(){
$(this).trigger('change');
});
Note in both cases we use keyup (onkeyup) to trigger the 'change' event. This allows the browser to show the changed class as soon as the user changes the value using a keyboard, rather than 'onblur'.

