cssText

In this chapter you will learn:

  1. Access the css text from cssText in Javascript
<!DOCTYPE HTML><!--   j a v a  2s .c om-->
<html>
<body>
  <p id="block1" style="color: white; background-color: gray">
    This is a test.
  </p>
  <button id="pressme">Press Me</button>
  <div id="placeholder"></div>
  <script>
    var placeholder = document.getElementById("placeholder");
    var targetElem = document.getElementById("block1");
    displayStyle();
    document.getElementById("pressme").onclick = function() {
      targetElem.style.cssText = "color:black";
      displayStyle();
    }
    function displayStyle() {
      if (placeholder.hasChildNodes()) {
        placeholder.removeChild(placeholder.firstChild);
      }
      var newElem = document.createElement("pre");
      newElem.innerHTML += targetElem.style.cssText;
      placeholder.appendChild(newElem);
    }
  </script>
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Change the color style with Javascript
Home » Javascript Tutorial » CSS
CSSStyleDeclaration Objects
border
padding
cssText
color
getComputedStyle
getPropertyValue
getPropertyCSSValue
getPropertyPriority