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!
2 ответов
+ 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.
+ 1
It worked, thanks for the help!