I’m trying to create a website using Brackets, whereby when you click on a button, it’ll show up on the console when you inspect it. I have already make a variable, but the variable doesn’t work when i put it as addEventListener.
Here’s part of the code from the html file:
<div class="logo">
<div>
<img class="logo-img" src="images/logo.jpg" alt="logo">
</div>
<div>
<img class="icon" id="bar" src="images/bars.svg" alt="">
<img class="icon" id="cross" src="images/cross.svg" alt="">
</div>
</div>
Here’s the code from the js file:
var bar = document.querySelector('#bar');
var cross = document.querySelector('#cross');
var nav = document.querySelector('.nav');
bar.addEventListener('click', () => {
console.log('button has been clicked')
})
I tried changing from const to var, because I had error when i create a variable using const. And when i tried doing the addEventListener using a variable to check whether a specific button is clicked by doing console.log(‘button has been clicked’)
Cheryl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.