Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js styles when clicked

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 :: regex match line that does not contain string 
Javascript :: javascript remove object property 
Javascript :: javascript remove underscore and capitalize 
Javascript :: jquery get meta value 
Javascript :: 3 letter months javascript array 
Javascript :: remove array elements javascript 
Javascript :: javascript is int in array 
Javascript :: get year from date javascript 
Javascript :: copy to clipboard javascript dom 
Javascript :: javascript strip 
Javascript :: validate phone number regex 
Javascript :: adding border in react native 
Javascript :: cypress input value should be 
Javascript :: nodejs mysql insert object query 
Javascript :: convert json object to array javascript 
Javascript :: js input type range get value on select 
Javascript :: angular show time ago 
Javascript :: reduce parameters 
Javascript :: anonymous function jquery 
Javascript :: 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 
Javascript :: ionic status bar color 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =