To get css property value with JavaScript you can use the getComputedStyle() method. It returns an object with the final used values of all the CSS properties of an element.

Syntax:
var style = window.getComputedStyle(elm, pseudoElt).propertyName;
- elm - The Element for which to get the computed style.
- pseudoElt - A string specifying the pseudo-element to match. Must be ommited (or null) for regular elements (The second argument, pseudoElt, is not supported in Internet Explorer and Opera browsers).
- propertyName - is the css property, in camelCase (e.g.: width, backgroundColor, fontSize, etc.).

Example, a button to get the CSS color and background-color properties value of a specified HTML element (JavaScript converts color code in RGB format).
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#div1 {
 width: 80%;
 height: 2.5em;
 margin: 1em;
 background-color: #02ed03;
 color: #0001da;
}
</style>
</head>
<body>
<h4>example getComputedStyle()</h4>

<div id='div1'>https://coursesweb.net/javascript/</div>
<button id='btn1'>Test</button>
<script>
// gets and alerts the css properties: color, background-color of the html element with ID pasased in 'eid'
function test(eid) {
 var elm = document.getElementById(eid);
 var css_color = window.getComputedStyle(elm, null).color;
 var css_bgcolor = window.getComputedStyle(elm, null).backgroundColor;

 alert('color: '+ css_color +'\n background-color: '+ css_bgcolor);
}

// register onclick event for #btn1, that calls the test() function
if(document.getElementById('btn1')) document.getElementById('btn1').onclick = function() { test('div1');};
</script>
</body>
</html>
• The getComputedStyle() method uses property name in camelCase, if you want to use property name as it is in CSS, apply the getPropertyValue() method.
Syntax:
var style = window.getComputedStyle(elm, pseudoElt).getPropertyValue('css-property');
Example, when click on a button, gets the CSS width and margin-top property values of a specified HTML element.
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#div1 {
 width: 80%;
 height: 2.5em;
 margin: 1em;
 background-color: #02ed03;
 color: #0001da;
}
</style>
</head>
<body>
<h4>example getPropertyValue</h4>

<div id='div1'>https://gamv.eu/jocuri/</div>
<button id='btn1'>Test</button>
<script>
// gets and alerts the css properties: width, margin-top of the html element with ID pasased in 'eid'
function test(eid) {
 var elm = document.getElementById(eid);
 var css_width = window.getComputedStyle(elm, null).getPropertyValue('width');
 var css_mtop = window.getComputedStyle(elm, null).getPropertyValue('margin-top');

 alert('width: '+ css_width +'\n margin-top: '+ css_mtop);
}

// register onclick event for #btn1, that calls the test() function
if(document.getElementById('btn1')) document.getElementById('btn1').onclick = function() { test('div1');};
</script>
</body>
</html>

Get CSS property value with jQuery

To get the css property value with jQuery, use the jQuery css() function.
- Example, when click on a button, gets the CSS height and background-color properties value of a specified HTML element.

<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Example jQuery css</title>
<style>
#div1 {
 width: 80%;
 height: 2.5em;
 margin: 1em;
 background-color: #02ed03;
 color: #0001da;
}
</style>
<script src='jquery_library.js'></script>
</head>
<body>
<div id='div1'>http://www.coursesweb.net/jquery-course</div>
<button id='btn1'>Test</button>
<script>
// gets and alerts the css properties: height, background-color of the html element with ID pasased in 'eid'
function test(eid) {
 var elm = $('#'+ eid);
 var css_height = elm.css('height');
 var css_bgcolor = elm.css('background-color');

 alert('height: '+ css_height +'\n background-color: '+ css_bgcolor);
}

// register onclick event for #btn1, that calls the test() function
if(document.getElementById('btn1')) document.getElementById('btn1').onclick = function() { test('div1');};
</script>
</body>
</html>
Demo:
http://coursesweb.net/jquery-course

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Get CSS property value with getComputedStyle ot jQuery

Last accessed pages

  1. Creating objects in ActionScript (7983)
  2. If Else conditionals, Comparative and Logical operators (3028)
  3. PHP SimpleXML (1926)
  4. Arrays in ActionScript 3 (1090)
  5. Get CSS property value with getComputedStyle ot jQuery (3351)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1435)
  2. Tabs effect with CSS (1370)
  3. Contact page - CoursesWeb (1367)
  4. Insert, Select and Update NULL value in MySQL (792)
  5. PHP getElementById and getElementsByTagName (622)