Variables in javascript

CSS Tutorial


Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

CSS Advanced


Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2298

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2302

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2308

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2312

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2316

Warning: Attempt to read property "post_status" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2320

Warning: Attempt to read property "ID" on null in /home/mydevhel/public_html/wp-admin/includes/template.php on line 2334

CSS Responsive

CSS Grid

CSS Change Variables with JavaScript

CSS variables have access to the DOM, that refers to that it can be changed with JavaScript.

Below is an example that creates a script to show and change the –blue variable

				
					<h1>Get and Change CSS Variable With JavaScript</h1>
<div class="container">
  <h2>Lorem Ipsum</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit.</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit.</p>
  <p>
    <button>Yes</button>
    <button>No</button>
  </p>
</div>
<br>
<button type="button" onclick="myFunction_get()">Get CSS Variable with JavaScript</button>
<button type="button" onclick="myFunction_set()">Change CSS Variable with JavaScript</button>
				
			
CSS
				
					:root {
  --blue: #1e90ff;
  --white: #ffffff; 
}

body {
  background-color: var(--blue);
}

h2 {
  border-bottom: 2px solid var(--blue);
}

.container {
  color: var(--blue);
  background-color: var(--white);
  padding: 15px;
}

.container button  {
  background-color: var(--white);
  color: var(--blue);
  border: 1px solid var(--blue);
  padding: 5px;
}
				
			

JAVASCRIPT

				
					<script type="rocketlazyloadscript">
// Get the root element
var r = document.querySelector(':root');
// Create a function for getting a variable value
function myFunction_get() {
  // Get the styles (properties and values) for the root
  var rs = getComputedStyle(r);
  // Alert the value of the --blue variable
  alert("The value of --blue is: " + rs.getPropertyValue('--blue'));
}

// Create a function for setting a variable value
function myFunction_set() {
  // Set the value of variable --blue to another value (in this case "lightblue")
  r.style.setProperty('--blue', 'lightblue');
}
</script>
				
			

Output

Get and Change CSS Variable With JavaScript

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit.