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 run shell script 
Javascript :: javascript remove property from object 
Javascript :: emoji mart npm 
Javascript :: Extract phone number from text regex 
Javascript :: javascript findindex 
Javascript ::  
Javascript :: javascript return promise 
Javascript :: javascript check typeof array 
Javascript :: sum all elements in array javascript 
Javascript :: javascript round to 1 decimal 
Javascript :: validate password javascript 
Javascript :: js getelementbyid 
Javascript :: jquery serialize with file upload 
Javascript :: moment get weekday name 
Javascript :: add comma after every 3 digits javascript 
Javascript :: js input type range get value while sliding 
:: node red admin password setting 
Javascript :: reduce parameters 
Javascript :: anonymous function jquery 
:: NodeJS get rootpath of our project 
Javascript :: owl carousel get started 
Javascript :: clear file upload jquery 
Javascript :: output in javascript 
Javascript :: get the element the cursor hovering over 
Javascript :: python range in javascript 
Javascript :: how to find length of array in javascript without using length method 
Javascript :: iterate over enum angular ngfor 
Javascript :: Select All Elements With A Class getElementsByClassName 
Javascript :: windows how to set process.env variables 
:: ionic status bar color 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =