lundi 17 octobre 2022

Change state of a checkbox with addEventListener in JavaScript

I'm really new to JavaScript. I'm having some issues with my JavaScript code to change the state (checked or unchecked) of my checkbox input every time I click on an anchor element (the size and content is correct in CSS).

HTML simplified

<section>
  <a id="trigger"><img src="images/logo.png"></a>
  <input type="checkbox" id="nav-toggle">
</section>

JavaScript

let trigger = document.getElementById('trigger');
trigger.addEventListener('click', function() {
  let i = document.getElementById('nav-toggle');
  if(i.checked === false) {
     i.checked = true;
  } else {
    i.checked = false;
    }
  }
});

Let me know if my JavaScript code make sense, and what should I fix to make it work. Thanks!




Aucun commentaire:

Enregistrer un commentaire