<!DOCTYPE html>
<html>
<head>
<title>My test</title>
<style>
.img {
display:inline-block;
border:dotted;
}
.button {
display:inline-block;
border:dotted;
}
</style>
<script>
//Loads all the elements and assigns event handlers
function load()
{
var root = document.getElementById("body");
var test = document.createElement("div");
test.setAttribute("class", "button");
test.setAttribute("id", "p");
test.innerHTML = "Prev"
root.appendChild(test);
var test = document.createElement("div");
test.setAttribute("class", "main");
test.innerHTML = "Anne"
root.appendChild(test);
var test = document.createElement("div");
test.setAttribute("class", "main");
test.innerHTML = "Anise"
root.appendChild(test);
var test = document.createElement("div");
test.setAttribute("class", "main");
test.innerHTML = "Anna"
root.appendChild(test);
var test = document.createElement("div");
test.setAttribute("class", "button");
test.setAttribute("id", "n");
test.innerHTML = "Next"
root.appendChild(test);
}
document.addEventListener("DOMContentLoaded", load, false)
</script>
</head>
<body>
</body>
</html>
This doesn’t produce anything on the page! Why?
You are looking for a DOM element with the
idattribute ofbody, when you really intend to get the body tag.You could also add an id attribute to the
<body>tag as<body id='body'>without having to modify your JavaScript, but this seems kind of inappropriate. Modify your script to usedocument.bodyinstead.Update
You are overwriting the
innerHTMLproperty ofbtnPrevin each group of lines. We don’t see wherebtnPrevis even defined in your markup. Perhaps each time you definevar test, you intend to definebtnPrev?