Log in

View Full Version : Help me with Javascript



akiloama
11-25-2020, 06:21 AM
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:


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);


<!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>
}

molendijk
11-27-2020, 05:10 PM
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:

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:

<!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>