Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to change css style on click

const elem = document.getElementById('elem-id');
// Now that we got the element we needed,
// there are 3 ways that we can style it when it's clicked

// method 1: .onclick
elem.onclick = () => {
  elem.style.backgroundColor = 'red';
}

// method 2: .addEventListener
elem.addEventListener('click', function(){
  elem.style.backgroundColor = 'red'; 
})

// method 3: function
function funcName(){
  elem.style.backgroundColor = 'red';
}
// now that we declared the function we need to call it on click,
// we can use both method 1 & 2, replace the code and call the function there
// or we can call it on the html attribute 'onclick'

// <div id="elem-id" onclick="funcName"></div>
Comment

PREVIOUS NEXT
Code Example
Javascript :: npm yarn run shell script 
Javascript :: jquery get td value 
Javascript :: remove from object javascript 
Javascript :: reverse a string without affecting special characters in javascript 
Javascript :: how to add up all numbers in an array 
Javascript :: javascript array delete by index 
Javascript :: semantic ui dropdown value react 
Javascript :: javascript get typeof array 
Javascript :: express.static 
Javascript :: get time from date 
Javascript :: validate phone number javascript 
Javascript :: vanilla javascript add class 
Javascript :: js two array combining with id 
Javascript :: javascript array flat 
Javascript :: javascript text word wrap replace 
Javascript :: for each jquery 
Javascript :: javascript minimum number in array 
Javascript :: javascript get image width and height 
Javascript :: vue js countdown timer 
Javascript :: set min date field to current date 
Javascript :: how to hash with crypto Node.js 
Javascript :: convert string time to time in javascript 
Javascript :: check if input has value javascript 
Javascript :: how to get last item in array javascript 
Javascript :: javascript allow only numeric characters 
Javascript :: button disabled javascript 
Javascript :: $post in jquery 
Javascript :: nodejs file exists 
Javascript :: convert string to number javascript 
Javascript :: js index sorted 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =