I know this is probably something simple, but I can’t figure it out. When I load the page, I see a flash of my text area box and the button and then the json data is diplayed on the page, replacing it. Why are the other elements disappearing?
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
</head>
<body>
<div id="wrapper">
<div id="chat">
<script type="text/javascript">
$(function(){
$.getJSON("process.php?jsoncallback=?", function(data){
for (var x = 0; x < data.length; x++) {
document.write(data[x].username);
document.write(': ');
document.write(data[x].comment);
document.write('<br/><br/>');
}
});
});
</script>
</div>
<div id="send">
<textarea id="send_text"></textarea>
<button>Send</button>
</div>
</div>
</body>
</html>
Thanks!
Because that’s what
document.writedoes. I’m guessing you actually want to append your content somewhere. You can use the jQueryhtmlortextmethods to set the content of an element.What is actually happening when you call
document.write? The MDN article explains it well:And the page on
document.opentells us this: