+ 1
Why this JavaScript code isn't working?
var test = document.getElementById('test'); test.innerHTML = 'hello'
4 Answers
+ 7
This is because the javascript is triggered faster than the html document has time to load.
Use the "onload" method for example like this:
onload = ()=>{
var test = document.getElementById('test');
test.innerHTML = 'hello';
}
+ 5
Solo Thank you very much! Your solution works.
+ 4
what dom element are you applying it to? it works for div, p, h1,h2 etc. it will not work for img.
also, try to avoid innerHTML unless you absolutely have to. use textContent instead
https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML