-
Help me with Javascript
I’m learning Javascript these days,using The book called Dom programming art
Here is the problem,I want to use JS to create content,but nothing happen when I open my html document. What’s the problem? Thank you
Here is the code below:
Code:
window.onload=function(){
var para=document.creatElement("p");
var txt=document.creatTextNode("hello world");
para.appendChild(txt);
var testdiv=document.getElementById("testdiv");
testdiv.appendChild(para);
HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Test</title>
</head>
<body>
<div id="testdiv">
</div>
<script type="text/javascript" src="scripts/example.js"></script>
</body>
</html>
}
-
Hello akiloama,
The curly brace you've put at the end of the HTML must be put at the end of the javascript. And 'creat' must be replaced with 'create' everywhere. So your code should look like this:
Javascript:
Code:
window.onload=function(){
var para=document.createElement("p");
var txt=document.createTextNode("hello world");
para.appendChild(txt);
var testdiv=document.getElementById("testdiv");
testdiv.appendChild(para);
}
HTML:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Test</title>
</head>
<body>
<div id="testdiv">
</div>
<script type="text/javascript" src="scripts/example.js"></script>
</body>
</html>