最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fix "Uncaught TypeError: Cannot read property 'click' of null" - Stack Ove

programmeradmin9浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 9

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>

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();
    }
);
发布评论

评论列表(0)

  1. 暂无评论