If you are asking how to append elements and remove them from a page without having to use document.write
, you can do so using document.createElement()
Consider this example:
The add
button will create a div
and then append it to another div
in the page called target
. remove
will remove the first child element of the target
div
.
<!DOCTYPE html>
<html>
<head>
<title>Div Controller</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script>
function main() {
document.getElementById("add").addEventListener("click", function() {
var newDiv = document.createElement("div");
newDiv.innerHTML = "I'm a div";
document.getElementById("target").appendChild(newDiv);
});
document.getElementById("remove").addEventListener("click", function() {
var target = document.getElementById("target");
if (target.firstChild) {
target.removeChild(target.firstChild);
}
});
}
window.onload = main;
</script>
</head>
<body>
<div id="target"></div>
<button id="add">Add</button>
<button id="remove">Remove</button>
</body>
</html>