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);
7 Answers
+ 5
+ 2
Ipang but in "educative" they thought me to create elements in this way
+ 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.
+ 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 ...
+ 1
Always put your JS code just before the body tag closes or the JS code will execute before the HTML code.
+ 1
Put your Js code in script tag and that script tag in body tag.
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