How I can toggle a boolean variable clicking a button

How can I toggle a variable by clicking a button in javascript

// Here is my variable
BOTTON.document.getElementById('bttn');

let cliked = false;

// I want to change to true when I press a button

BOTTON.addEventListener('click', ()=>{
  

}

  • 1

    cliked = !cliked; ? What have you tried and what didn’t work as expected?

    – 




  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it’s currently written, it’s hard to tell exactly what you’re asking.

    – 
    Bot

Like this:

// Here is my variable
let clicked = false;

const button = document.getElementById('bttn');

button.addEventListener('click', () => {
  // Toggle the boolean variable
  clicked = !clicked;
  console.log(clicked);
});
<!DOCTYPE html>
<html>
<head>
  <title>Button Example</title>
</head>
<body>
  <button id="bttn">Click Me</button>
</body>
</html>

Leave a Comment