Why doesn't document.getElementbyId work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why doesn't document.getElementbyId work?

Hi, i have a problem using document.getElementbyId. It doesn't show "test" and I get an error: Uncaught ReferenceError: Invalid left-hand side in assignment. I have tried putting the script tag in various places, but it doesn't work. This is my html code: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Test</title> <script src="test.js"></script> </head> <body> <p id="demo"></p> </body> </html> This is my Javascript code: document.getElementById("demo") = "test"; I would appreciate any kind of help!

9th Apr 2020, 1:29 PM
Zah
Zah - avatar
2 Answers
+ 4
Change `innerHTML` like this document.getElementById("demo").innerHTML = "test"; document.getElementById("test") returns an element identified by 'id' attribute "demo". But you can't assign value for the element. You can modify the element's attribute though e.g. it's innerHTML or style.
9th Apr 2020, 1:50 PM
Ipang
+ 1
It worked, thanks for the help!
9th Apr 2020, 2:10 PM
Zah
Zah - avatar