Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js style

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

JavaScript Style Guide

firstName = "John";
lastName = "Doe";

price = 19.90;
tax = 0.20;

fullPrice = price + (price * tax);
Comment

js style

const title = document.querySelector('h1')

//title.setAttribute('style', 'margin: 50px');

console.log(title.style); // Show all style propertyes
console.log(title.style.color); // Log a color in console

title.style.margin = '50px'; // Set a margin of title
title.style.color='crimson'; // Set a color of title
title.style.fontSize = '60px'; // Set font size of title
Comment

Add style to the HTML document with JS

document.head.insertAdjacentHTML("beforeend", `<style>body{background:red}</style>`)
Comment

style through javascript

element.style.backgroundColor = "red";   // set the background color of an element to red
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript add inline style css var 
Javascript :: Finding Value of Promise With Then Syntax 
Javascript :: js list random order 
Javascript :: create javascript set 
Javascript :: react moment calendar times 
Javascript :: javascript make http request 
Javascript :: javascript Rename in the import file 
Javascript :: javascript map replace key value 
Javascript :: table checkbox react 
Javascript :: node js serve pdf file 
Javascript :: js some 
Javascript :: react datepicker documentation 
Javascript :: jquery get last element with two class name 
Javascript :: rxjs takeuntil 
Javascript :: how to put dynamic image in react 
Javascript :: blockchain javascript 
Javascript :: firebase get subcollection 
Javascript :: combine csv files javascript 
Javascript :: axios send payload in get request 
Javascript :: fs readfile promise 
Javascript :: wow.js 
Javascript :: airbnb and eslint react native 
Javascript :: asynchronous javascript 
Javascript :: react native image picker 
Javascript :: JavaScript try...catch...finally Statement 
Javascript :: The ".charAt()" JavaScript string method 
Javascript :: timezone in react js 
Javascript :: replace spaces with dashes 
Javascript :: send an email react native 
Javascript :: javascript in python 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =