+ 1

Please help me in this Js problem.

let section = document.getElementById("box"); let div; let div = document.createElement("div"); div = `<div>Hello</div>`; section.appendChild(div); // //I am trying to appendChild() the created div. ///The code says "Parameter 1 is not type of node" I am a beginner please help me.

6th Mar 2021, 2:20 PM
Asif Mahmud
Asif Mahmud - avatar
4 Respostas
0
div.innerHTML=`<div>Hello</div>` . What you are trying to do is assigning a string to div object which basically creates a new object i.e. string.
6th Mar 2021, 2:33 PM
Abhay
Abhay - avatar
0
if you are trying to append <div>Hello</div> to <section> you could just write: let section = document.getElementById("box"); let div = document.createElement("div"); div.textContent = `Hello`; section.appendChild(div); if you want to rather append <div><div>Hello</div></div>, you could follow Abhay answer... if you just want to put some text in section content, you could just do: let section = document.getElementById("box"); section.textContent = 'Hello'; you could also append text nodes explicitly (element.appendChild(document.createTextNode('Hello'))), but that's not worth it ;P
6th Mar 2021, 2:44 PM
visph
visph - avatar
0
obviously, you could also append the html string directly to the section element through innerHTML, but that's not the best way: let section = document.getElementById("box"); section.innerHTML = `<div>Hello</div>`; ... despite it is shorter, it is more error prone and less efficient than appending nodes, if your html string becomes big ;)
6th Mar 2021, 2:49 PM
visph
visph - avatar
0
Thank you guys...
6th Mar 2021, 3:21 PM
Asif Mahmud
Asif Mahmud - avatar