It is a correct behaviour. You getting the HTML string using DOM (Document Object Model), which reflects the current HTML page you are seeing in the browser (including whatever changed)
If you need to retain the old value of the text box, you need to:
- Either, store the original value of the textbox upon page loaded for comparing later
- Or, only if you need the real HTML source code, you can do either:
- Make sure your code is the first bit of code run by the browser, it might be tricky to get a cross-browser solution (have a look at this question), then in your code, store a copy of the
document.body.outerHTML
for later use.
- Or, initiate a request back to the current URL and store the return body as HTML source code, this way you are sure that you have a copy of the original HTML source code, but it costs you 1 additional request.
//jQuery code
$.get(window.location.href, function (resp) {
// store the resp.data on some object
var sourceHTML = resp.data;
})
.fail(function (resp) {
// error handling
});
Added comment of Paul S below
The document.body.outerHTML
will contain only the <body>
section. This
(new XMLSerializer).serializeToString(document)
will give you the whole <html>
, including the <!DOCTYPE html>