I have used the code from reference Generic way to detect if html form is edited
$.fn.extend({
trackChanges: function () {
$(":input", this).change(function () {
$(this.form).data("changed", true);
});
},
isChanged: function () {
return this.data("changed");
}
});
and using it like
$("#check").click(function () {
$("#myform").trackChanges();
if ($("#myform").isChanged()) {
alert("changed");
} else {
alert("Not changed");
}
});
But I dont understand how to catch the not changed event, whenever I click on button it is alerting changed, I need to catch the not changed event also. Control is not going to else part
HTML
<form id="myform">
<input type="text" id="id1" />
<select id="id2" ><option>1</option><option>2</option></select>
<textarea>asdasd</textarea>
<input type="button" id="check" value="button"/>
</form>
$.fn.extend({
trackChanges: function () {
$(":input", this).change(function () {
$(this.form).data("changed", true);
});
},
isChanged: function () {
return this.data("changed");
}
});
<form id="myform">
<input type="text" id="id1" />
<select id="id2"><option>1</option><option>2</option></select>
<textarea>asdasd</textarea>
<input type="button" id="check" value="button" />
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
$("#myform").trackChanges();
$("#check").click(function () {
if ($("#myform").isChanged()) {
alert("vam");
} else {
alert("no");
}
});
});
</script>