Faqts : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

31 of 35 people (89%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I manipulate a style class?

Jun 28th, 2000 07:33
Martin Honnen,


With the cascading nature of css there is no unique style class object 
as there can be several rules for a style class but that allows you to 
simply add another rule for the style class if you want to change a 
property:
<HTML>
<HEAD>
<STYLE>
.aClass {
  color: white;
  background-color: orange;
}
</STYLE>
<SCRIPT>
function setClassProperty (className, property, value) {
  if (document.all) {
    var lastStyleSheet = 
      document.styleSheets[document.styleSheets.length - 1];
    var selector = '.' + className;
    var css = property + ': ' + value + ';';
    lastStyleSheet.addRule(selector, css);
  }
  else if (document.getElementById) {
    var lastStyleSheet = 
      document.styleSheets[document.styleSheets.length - 1];
    var ruleText = '.' + className + ' { ' + property + ': ' + value 
+ '}';
    lastStyleSheet.insertRule(ruleText, lastStyleSheet.cssRules.length);
  }
}
</SCRIPT>
</HEAD>
<BODY>
<BUTTON ONCLICK="setClassProperty('aClass', 'display', 'none')">
hide
</BUTTON>
<BUTTON ONCLICK="setClassProperty('aClass', 'display', 'block')">
show
</BUTTON>
<P CLASS="aClass">
Kibology for all.
</P>
<DIV CLASS="aClass">
All for Kibology.
</DIV>
<P CLASS="aClass">
JavaScript.FAQTs.com
</P>
</BODY>
</HTML>