3
I’m setting the function to open a menu. But I am getting the error message "Uncaught Typeerror: Cannot read Property 'addeventlistener' of null"
Below is the code I’m using to open the menu with JS
var veri = 1;
var trigger = document.getElementById('menu-trigger').addEventListener("click",function(){
var menu = document.getElementById('menu-hidde');
if (veri == 1) {
menu.style.left = "0px";
veri = 0;
}else{
menu.style.left = "-100%";
veri = 1;
}
})
And also the HTML code where I quote the same
<!DOCTYPE html>
<html lang"en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="estilos.css" media="all" />
<script type="text/javascript" src="teste.js"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
<title>Styles Conference</title>
</head>
<body>
<div class="menu-trigger" id="menu-trigger">
<div></div>
<div></div>
<div></div>
</div>
<nav class="menu-hidde" id="menu-hidde">
<ul>
<li>teste</li>
<li>teste</li>
<li>teste</li>
<li>teste</li>
</ul>
</nav>
How can I fix this?
Look, I don’t know if your problem is here on the snippet or in a real app, but here on the snippet you posted, it’s two separate snippets. That is, js does not find the id it sent because they are not in the same context...
– DiegoSantos
@Diegosantos I’m learning and found the codes in a snippet on the internet. It works perfectly but when I wrote it in Note Pad ++ presented this error.
– dotSonic
Well, testing on a single snippet here worked for me. Show all the source your file... Other detail, did you call js before html or after? Prefer to call later or declare it in the header...
– DiegoSantos
If applicable, use the window load event.
– DiegoSantos
@Diegosantos updated my post showing my html completely, here is the link where I am taking the reference of the code https://preloadweb.wordpress.com/2016/03/09/menu-retratil-com-css-e-javascript/
– dotSonic
Is your script the correct test.js? Replace with this and say what happens: '<script type="text/javascript" src="test.js" async></script>'
– DiegoSantos
@Diegosantos It worked!! Thank you very much, where was my mistake?
– dotSonic
I will answer... If you can dial as an answer, thank you
– DiegoSantos
@dotSonic No,
ascyn
does not load later as Diego stated, he is mistaken ... the Async carries "asynchronously", which means there is no order, it may be that it works and may have occasion that fails, the correct is to use isdefer
orwindow.onload
orDOMContentLoaded
– Guilherme Nascimento
@Guillhermenascimento, see that I put a comma "In this case" in my reply. This justifies my explanation.
– DiegoSantos
Diego think this wrong (the downvote nay is mine) Async is not last, neither in this case nor in any, async is asynchronous, loads without defined order, as I explained in my reply, if the HTML is large you may have https://answall.com/a/289479/3635 - its response worked in the case of AP by coincidence only, in other environments or variations of download speed or small delays in web servers, ascyn can surely fail.
– Guilherme Nascimento
Agree @Guilhermenascimento. In fact async loads asynchronously. But as I said in this answer, exclusive to this problem, it carries last. Precisely because it is asynchronous. And it’s okay to downvote. Just like you’re here to add the community. Scoring is important to give us privilege. That’s why I asked in this case. But that’s part :D
– DiegoSantos
No Diego, no last load, variations on the network and/or server will surely cause side effects, no order will not load exactly last, only last load because it is probably still testing on localhost, the moment you go to production (real site) and someone opening in a 3G with some oscillation will probably trigger the failure Cannot read Property 'addeventlistener' of null
– Guilherme Nascimento