The Sysadmin Notebook  

Sitemap

Example: Accessing the style collection

Contact Details

Awesome Web Production Company
Going Nowhere Lane 0
Catch 22
N4 2XX
England

Rather than set the style of an element in javascript, we can instead add or remove a class for the element, and then target this class with CSS to create dynamic effects. The javascript for this page:

sc={
  // CSS classes
  hidingClass:'hide', // hide elements

  init:function(){
    sc.head=document.getElementsByTagName('h3')[0];
    if(!sc.head){return;}      
    sc.ad=DOMhelp.closestSibling(sc.head,1);
    DOMhelp.cssjs('add',sc.ad,sc.hidingClass);
    var t=DOMhelp.getText(sc.head);
    var collapseLink=DOMhelp.createLink('#',t);
    sc.head.replaceChild(collapseLink,sc.head.firstChild);
    DOMhelp.addEvent(collapseLink,'click',sc.peekaboo,false)
    collapseLink.onclick=function(){return;} // Safari fix
  },
  peekaboo:function(e){
    if(DOMhelp.cssjs('check',sc.ad,sc.hidingClass)){
       DOMhelp.cssjs('remove',sc.ad,sc.hidingClass)
    } else {
       DOMhelp.cssjs('add',sc.ad,sc.hidingClass)
    }
    DOMhelp.cancelClick(e);
  }
}
DOMhelp.addEvent(window,'load',sc.init,false);

The additional CSS is as follows:

.hide{
  position:absolute;
  top:0;
  left:-9999px;
  height:0;
}
.show{
  position:relative;
  top:0;
  left:0;
  height:auto;
}