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?

46 of 49 people (94%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do you create a hierarchical, collapsible list with DOM methods?

Mar 24th, 2009 15:43
chat alarab, J V, Martin Honnen, Pat Friedl,


The following sets up a hierarchical collapsible list for IE5 (it could 
be adapted for IE4 but I lack an IE4 installation currently to test it)
(The code does not work on IE4.0)and NN6 (or NN6 if marketing hype 
prevails) by using HTML nested UL and 
LI elements where the display style property is toggled between none 
and block. 
<HTML>
<HEAD>
<STYLE>
.expandable {
  list-style-image: 
url(http://msdn.microsoft.com/msdn-online/shared/graphics/plus.gif);
}
.leaf {
  list-style-image: 
url(http://msdn.microsoft.com//msdn-online/shared/graphics/solid.gif);
  cursor: default;
}
.subList {
  display: none;
}
</STYLE>
<SCRIPT>
function getSubList (li) {
  var subList = null;
  for (var c = 0; c < li.childNodes.length; c++)
    if (li.childNodes[c].nodeName == 'UL') {
      subList = li.childNodes[c];
      break;
    }
  return subList;
}
function onClickHandler (evt) {
  var target = evt ? evt.target : event.srcElement;
  if (target.className == 'expandable') { //
    if (target.expanded) {
      target.style.listStyleImage = 
'url(http://msdn.microsoft.com/msdn-online/shared/graphics/plus.gif)';
      getSubList(target).style.display = 'none';
      target.expanded = false;
    }
    else {
      target.style.listStyleImage = 
'url(http://msdn.microsoft.com/msdn-online/shared/graphics/minus.gif)';
      getSubList(target).style.display = 'block';
      target.expanded = true;
    }
  }
  return true;
}
document.onclick = onClickHandler;
</SCRIPT>
</HEAD>
<UL>
  <LI CLASS="expandable">
    Gods
    <UL CLASS="subList">
      <LI CLASS="expandable">
        NetGods
        <UL CLASS="subList">
          <LI CLASS="leaf">
            Kibo
          </LI>
          <LI CLASS="leaf">
            Xibo
          </LI>
        </UL>
      </LI>
      <LI CLASS="leaf">
        Jehovah
      </LI>
      <LI CLASS="leaf">
        Allah
      </LI>
    </UL>
  </LI>
  <LI CLASS="expandable">
    Religions
    <UL CLASS="subList">
      <LI CLASS="expandable">
        Net Religions
        <UL CLASS="subList">
          <LI CLASS="leaf">
            Kibology
          </LI>
          <LI CLASS="leaf">
            Xibology
          </LI>
        </UL>
      </LI>
      <LI CLASS="leaf">
        Jerkianity
      </LI>
      <LI CLASS="leaf">
        Illame
      </LI>
    </UL>
  </LI>
</UL>
</BODY>
</HTML>
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com
.