Reputation:
I'm just starting to code an extension and I've encountered a problem. I'm using "getElementById" to see if this is the correct webpage. And I'm always no matter what the same result "null". Can someone help me, please? Here is the code:
var abc = document.getElementById('questionText');
console.log(abc);
if (abc == null) {
alert("not working");
} else {
alert("works");
}
Upvotes: 0
Views: 64
Reputation: 55200
Wrap the code inside DOMReady, otherwise you might end up looking for the element before HTML document has been completely loaded and parsed.
document.addEventListener('DOMContentLoaded', function() {
var abc = document.getElementById('questionText');
console.log(abc);
if (abc == null) {
alert("not working");
} else {
alert("works");
}
});
Upvotes: 1