As I've been asked by a few friends of mine about Expand/Collapse, I just wrote a small tutorial of it even though you can find the code in page source of my old blog template it's not easy to follow because the code was lengthy and the function I wrote is not well structured I would say.
<html>
<head>
<script language="javascript">
// TODO: expand/collapse (show/hide) an element, specified by id,
// triggered by an element, specified by triggerID. triggerTxt is a text in trigger element.
function toggle(id, triggerId, triggerTxt){
e = document.getElementById(id);
trigger = document.getElementById(triggerId);
if (e.style.display == 'block' || e.style.display != 'none'){
e.style.display = 'none'
// instead of [+] you can also replace with an image as well
trigger.innerHTML = "[+] " + triggerTxt;
}
else
{
e.style.display = 'block'
// instead of [-] you can also replace with an image as well
trigger.innerHTML = "[-] " + triggerTxt;
}
}
</script>
</head>
<body>
<h3 id="trigger1" onclick="toggle('list1','trigger1','click here')">click here</h3>
<ul>
<!-- element (in div) to be expanded/collapsed -->
<div id="list1">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</div>
<ul>
<!-- call this function now to collapse list1 element when the page is loaded -->
<script>toggle('list1','trigger1','click here')</script>
</body>
Continue reading...