1document.body.onload = addElement;
2
3function addElement () {
4 // create a new div element
5 const newDiv = document.createElement("div");
6
7 // and give it some content
8 const newContent = document.createTextNode("Hi there and greetings!");
9
10 // add the text node to the newly created div
11 newDiv.appendChild(newContent);
12
13 // add the newly created element and its content into the DOM
14 const currentDiv = document.getElementById("div1");
15 document.body.insertBefore(newDiv, currentDiv);
16}
1Your code works well you just mistyped this line of code:
2
3document.getElementbyId('lc').appendChild(element);
4
5change it with this: (The "B" should be capitalized.)
6
7document.getElementById('lc').appendChild(element);
8HERE IS MY EXAMPLE:
9
10<html>
11<head>
12
13<script>
14
15function test() {
16
17 var element = document.createElement("div");
18 element.appendChild(document.createTextNode('The man who mistook his wife for a hat'));
19 document.getElementById('lc').appendChild(element);
20
21}
22
23</script>
24
25</head>
26<body>
27<input id="filter" type="text" placeholder="Enter your filter text here.." onkeyup = "test()" />
28
29<div id="lc" style="background: blue; height: 150px; width: 150px;
30}" onclick="test();">
31</div>
32</body>
33
34</html>
1var btn = document.createElement("BUTTON"); // Create a <button> element
2btn.innerHTML = "CLICK ME"; // Insert text
3document.body.appendChild(btn); // Append <button> to <body>
1document.body.onload = addElement;
2
3function addElement () {
4 // create a new div element
5 var newDiv = document.createElement("div");
6 // and give it some content
7 var newContent = document.createTextNode("Hi there and greetings!");
8 // add the text node to the newly created div
9 newDiv.appendChild(newContent);
10
11 // add the newly created element and its content into the DOM
12 var currentDiv = document.getElementById("div1");
13 document.body.insertBefore(newDiv, currentDiv);
14}