I have an html document
<html>
<head></head>
<body>
<form>
<input type="text" value="" />
</form>
<input type="button" id="doit" value="do it"/>
<!-- jQuery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#doit").click(function(){
var dom = document.documentElement.innerHTML;
alert(dom);
});
});
</script>
</body>
</html>
if I edit the text field its value is still the original blank value in the alert…WHY? and how do I see the actual DOM in a string?
You’d need to set each attribute explicitly with the current value: http://jsfiddle.net/GW8eU/.
It’s rather dirty and I do not see a real rationale behind outputting the DOM to a string, but it seems to answer your question.
One of the reasons it’s dirty is that the
tryblock is essential, because otherwise you’d be setting thetypeattribute of aninputwhich is immutable. I’m pretty certain this won’t work with other special attributes either, but for thevalueof aninputit works fine.Therefore I would still like to know what’s the reason behind wanting the DOM as a string; probably there is a more elegant solution.