Please help me to solve this error [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please help me to solve this error [solved]

//html <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="p"> </div> </body> </html> //Js var k=document.createElement('h1'); k.innerHTML="text added"; var c=document.querySelector('#p'); c.appendChild(k);

11th Dec 2021, 8:19 AM
Samaan Sayed
7 Answers
+ 5
Pratik Jayakrishna🇮🇳 Kashyap Kumar ⭕ Ipang thank you for helping me out
12th Dec 2021, 6:22 AM
Samaan Sayed
+ 2
Ipang but in "educative" they thought me to create elements in this way
11th Dec 2021, 8:32 AM
Samaan Sayed
+ 1
You don't have a <h1> element, the body only contains 1 element, a <div>. You can't make variable <k> to refer a non existing element.
11th Dec 2021, 8:24 AM
Ipang
+ 1
Sorry Anjali, I misread your script, I thought it was referencing a <h1>. What error though? I didn't get any error here ... If you put the script in JS tab, and you're using SoloLearn app ... window.onload = () => { // move the script in here } It shouldn't be a problem if you were using SoloLearn web ...
11th Dec 2021, 8:38 AM
Ipang
+ 1
Always put your JS code just before the body tag closes or the JS code will execute before the HTML code.
11th Dec 2021, 9:40 AM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
Put your Js code in script tag and that script tag in body tag.
12th Dec 2021, 6:13 AM
Pratik
Pratik - avatar
0
//Unsolved? <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="p"> </div> <sScript> var k=document.createElement('h1'); k.innerHTML="text added"; var c=document.querySelector('#p'); c.appendChild(k); </sScript> </body> </html> //remove S in sScript. Sololearn not allow script tag to add, so I added S
11th Dec 2021, 10:10 AM
Jayakrishna 🇮🇳