Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Uncaught TypeError: Cannot read property 'click' of null"

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.com/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>
like image 615
Jason Owens Avatar asked May 13 '19 03:05

Jason Owens


1 Answers

In 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>
like image 51
random Avatar answered Sep 28 '22 07:09

random