+ 3
Why setAttribute() doesn't work here?
2 Respostas
+ 10
The JS error is due to the script executing before the document is properly loaded. Try:
window.onload = function() {
var h1 = document.getElementsByTagName("h1")[0]
h1.setAttribute("title","good");
}
+ 5
Your DOM is not ready at the moment.
Wrap your code with
document.addEventListener('DOMContentLoaded', function(){
//Your code
});