ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

JavaScript toggle class

 


<!DOCTYPE html>
<html>
<head>

<style>

.class1{color:red;}
.class2{color:green;}

</style>

</head>
<body>

<button id="toggleClass">Toggle Class</button>
<h1 class="class1">Click the Toggle Calss button to class of this element</h1>

<script>
const showMenu = document.querySelector('#toggleClass');
showMenu.addEventListener('click', e => {
document.querySelector('.class1').classList.toggle('class2')
})
</script>

</body>
</html>

Categroy:
javascripts