Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

onclick add class javascript

<div class="cl1">test</div>
<button onlick="change()">change</button>

// javaScript part 
let elemnt = document.getElementsByClassName("cl1")
function change(){
// "add" is gonna add classe one time if you click again class is not gonna remove if you want to change class and remove it with 2 click use toggle
// add
  elemnt.classList.add("cl2")
// toggle
  elemnt.classList.toggle("cl2")
}
 
PREVIOUS NEXT
Tagged: #onclick #add #class #javascript
ADD COMMENT
Topic
Name
9+2 =