I have a <form> and need to detect if anything therein was changed when it is submitted.
My idea was the following: upon loading the form, I'd save its content's .html() as a string in the form's .data() attribute. Unfortunately, changes do not show up when the form is submitted (please look at the snippets console).
A few things to consider:
I need to know if the content is actually different, not just whether something was changed but then changed back
The form has a LOT of different and dynamic elements, so while extracting the information via
.val(),.prop('checked'), etc. is possible, I'd really rather compare two strings
$('form').data('data', {oldState: $('form').html()});
$('form').on('submit', e => {
e.preventDefault();
var oldState = $('form').data('data').oldState;
var currState = $('form').html();
console.log(oldState);
console.log(currState);
console.log(oldState == currState);
if(oldState == currState) {
$('body').append('<p>same!</p>');
} else {
$('body').append('<p>changed!</p>');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="text" value="Old text">
<input type="checkbox" checked>
<button>Save</button>
</form>