Search
 
SCRIPT & CODE EXAMPLE
 

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")
}
Comment

add class on javascript onclick function

document.addEventListener('click', function handleClick(event) {
  console.log('user clicked: ', event.target);

  event.target.classList.add('bg-yellow');
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: js array intersection object 
Javascript :: next js active nav link 
Javascript :: javascript call function on click give element id 
Javascript :: change background image through props 
Javascript :: radio button default checked react 
Javascript :: vim react snippets 
Javascript :: How to clear localStorage when browser/tab is closing 
Javascript :: insert condition in a object javascript 
Javascript :: generate unique id javascript 
Javascript :: iife arrow function 
Javascript :: asyncstorage react native 
Javascript :: angular output 
Javascript :: javascript object total 
Javascript :: https with express 
Javascript :: html javascript redirect 
Javascript :: redirect function in javascript 
Javascript :: js object contain key 
Javascript :: javascript open in new window not tab 
Javascript :: linking in react native 
Javascript :: js regex i modifier 
Javascript :: transition event listener does not work 
Javascript :: javascript create array of objects with map 
Javascript :: filter array of objects to remove duplicate keys 
Javascript :: json schema string or null 
Javascript :: js get childrens 
Javascript :: await fetch in react 
Javascript :: p5.js create button 
Javascript :: type of variable js 
Javascript :: how to loop object javascript 
Javascript :: copy to clipboard javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =