Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript style onclick

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 :: .call javascript 
Javascript :: regex not contain 
Javascript :: how to remove key value pair from object js 
Javascript :: javascript add line from file to array 
Javascript :: jquery add style background-image 
Javascript :: leaflet center map 
Javascript :: semantic ui dropdown value 
Javascript :: typeof array javascript 
Javascript :: pdf with puppeteer 
Javascript :: how to remove duplicate values in array of objects using javascript 
Javascript :: jquery focus 
Javascript :: Jquery handle change event of file upload created dynamically 
Javascript :: .ignore file nodejs 
Javascript :: how to fix header on scroll 
Javascript :: js text word wrap 
Javascript :: compare two dates using moment 
Javascript :: mongodb update many 
Javascript :: get full month from date javascript 
Javascript :: on enter key press react js 
Javascript :: uncheck a checkbox in javascript 
Javascript :: what is ngmodel property binding 
Javascript :: react native responsive font 
Javascript :: javascript extract number from string 
Javascript :: show hide more text jquery 
Javascript :: getting state in react-router-dom v6 
Javascript :: lodash remove element from array 
Javascript :: regex pattern for strong password 
Javascript :: how to reload window in javascript 
Javascript :: add event listener on width screen resize 
Javascript :: How to disable reactive form submit button in Angular 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =