Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js onclick change styles

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

js onclick change css

<div id="foo">hello world!</div>
<img src="zoom.png" id="image" />
<script>
  $('#image').click(function() {
    $('#foo').css({
        'background-color': 'red',
        'color': 'white',
        'font-size': '44px'
    });
});
  </script>
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 :: remove an element from an array 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 
Javascript :: node red admin password setting 
Javascript :: json enum string 
Javascript :: javascript string to float 
Javascript :: jquery div element find and remove 
Javascript :: sequelize dialect 
Javascript :: convert time string in javascript 
Javascript :: check if any property of object is null javascript 
Javascript :: react native import svg image 
Javascript :: how to trap js errors window.onerror 
Javascript :: [Error - 10:52:45 PM] Failed to load jshint library 
Javascript :: typescript how to mode json files when compile 
Javascript :: error vuejs from chokidar enospc 
Javascript :: prevent default jquery 
Javascript :: javascript text new line 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =