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', ()=>{
}
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>
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