After very useful advices and tips found here I came up with this snippet.
I would like to check if form fields were changed. If true, show an alert icon with a tooltip in page top, if false or returned to field default value, hide the alert icon. With this script I could accomplish it, but I would like to know if this is the proper way and if it could be improved. Newbie here :)
In addition to what already made I also would like to mark the field that was changed (example: change the field label class).
$(':input').on('keyup keydown blur change', function () {
var isDirty = false;
$(':input').each(function () {
var $e = $(this);
if($e.is(':input[type=radio]') && $e.prop('defaultChecked') != $e.prop('checked')) {
isDirty = true;
} else if($e.is('select') && !this.options[this.selectedIndex].defaultSelected) {
isDirty = true;
} else if($e.is(':input[type=text], :input[type=url], :input[type=email], textarea') && $e.prop('defaultValue') != $e.val()) {
isDirty = true;
}
if(isDirty == true){
$("#toolbar-warning span").attr({
'class': 'btn active hasTooltip icon-warning-2',
'data-original-title': 'has changes!'
});
} else {
$("#toolbar-warning span").attr({
'class': 'btn hasTooltip icon-warning-2',
'data-original-title': ''
});
}
})
});
eachand for example I changed 2 fields values and after return one of those fields to it's default value the 'has changes!' warning disappears, and it still has one field changed.