I am experimenting with HTML and JavaScript. The following code should print something when entering a keystroke in a textbox:
<html>
<body>
<input type="text" id="commandInput" name="command" size="50" />
<script type="text/javascript">
var commandInput = document.getElementById("commandInput");
commandInput.onkeydown = function (evt) {
document.writeln("Test");
};
</script>
</body>
</html>
For some reason the textbox disappears when entering a keystroke, leaving nothing but a white page.
Any ideas why this is happening?
You shouldn’t use
document.writeordocument.writelnafter the page has loaded. Calling it after the page has loaded (in Firefox, at least) does an automatic call todocument.open(), which wipes out the current document.If you’re using it as a debugging tool, I’d suggest using Firebug and
console.log()instead.