I'm developing a Chrome Extension and getting the error "Uncaught TypeError: Cannot read property 'click' of null". It works in the console and I made sure to close the console. I'm trying to run this on the site below.
.html
popup.js
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', int);
});
function int() {
var source = document.getElementById('j1_64_anchor').click;
}
var coll = document.getElementsByClassName("collapsible");
var i;
popup.html
<body>
<button class="collapsible">Interfaces</button>
<div class="content">
<button onclick="int()" class="collapsible">Interfaces</button>
</div>
</body>
I'm developing a Chrome Extension and getting the error "Uncaught TypeError: Cannot read property 'click' of null". It works in the console and I made sure to close the console. I'm trying to run this on the site below.
https://realtime.demo.sonicwall./main.html
popup.js
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', int);
});
function int() {
var source = document.getElementById('j1_64_anchor').click;
}
var coll = document.getElementsByClassName("collapsible");
var i;
popup.html
<body>
<button class="collapsible">Interfaces</button>
<div class="content">
<button onclick="int()" class="collapsible">Interfaces</button>
</div>
</body>
Share
Improve this question
asked May 13, 2019 at 3:51
Jason OwensJason Owens
5432 gold badges6 silver badges21 bronze badges
3 Answers
Reset to default 9In your html
there is no element with id j1_64_anchor
. So your code document.getElementById('j1_64_anchor').click();
will fail.
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', int)
});
function int() {
console.log('calling');
var source = document.getElementById('j1_64_anchor').click();
}
var coll = document.getElementsByClassName("collapsible");
var i;
<button class="collapsible">Interfaces</button>
<div class="content" id="j1_64_anchor">
<button onclick="int()" class="collapsible">Interfaces</button>
</div>
Seems this line of code
function int() {
var source = document.getElementById('j1_64_anchor').click;
}
is getting executed before the dom is ready. Try by putting it inside DOMContentLoaded
event
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', int);
function int() {
var source = document.getElementById('j1_64_anchor').click;
}
var coll = document.getElementsByClassName("collapsible");
var i;
});
Wait until the DOM content is enabled:
window.addEventListener('DOMContentLoaded',
function() {
document.getElementById('j1_64_anchor').click();
}
);