Here is the first part without cookies...
<dl>
<dt><a onclick="switchMenu('myvar');">Swtich link</a></dt>
<dd id="myvar">Content</dd>
</dl>
function switchMenu(obj) {
var el = document.getElementById(obj);
if ( el.style.display!= 'none' ) {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}
Try this one out. No ids used. It could be part of a cookie-based persistence system, if the cookie stored the index of the <dd> element in the list.
Next one:-
To elaborate on my previous post, here's a quick and dirty but working example. It uses a UL for the menu. The function showMenu loops through the collection of LIs and shows or hides them. The (clickable) listitem with class="header" will always be shown.
<script type="text/javascript">
<!--
var aPrefix = 'pr';
function getCurrentState(mID) {
// returnvalues: 1 (closed), 2 (opened)
return document.cookie.substr(document.cookie.indexOf(mID+'=') + mID.length +1, 1);
}
function initMenu(mID) {
// make the header clickable, assign an ID
var kids = document.getElementById(mID).childNodes;
for (var i = 0; i < kids.length; i++) {
if (kids[i].className == 'header') {
kids[i].onclick = toggleMenu;
kids[i].id = aPrefix + mID;
}
}
// show or hide the menu
showMenu(mID);
}
function showMenu(mID) {
var currentState = getCurrentState(mID);
document.getElementById(aPrefix+mID).title = (currentState == 1? 'show' : 'hide') + ' this menu';
document.getElementById(aPrefix+mID).style.cursor = 'pointer';
var kids = document.getElementById(mID).childNodes;
for (var i = 0; i < kids.length; i++) {
if (kids[i].tagName == 'LI' && kids[i].className!= 'header') {
kids[i].style.display = currentState == 1? 'none' : 'block';
}
}
}
function toggleMenu(e) {
if (window.event) e = window.event;
var mID = e.srcElement? e.srcElement.id : e.target.id;
mID = mID.substr(aPrefix.length);
// write the cookie
var oneyear = new Date(); oneyear.setFullYear(oneyear.getFullYear() + 1);
document.cookie = mID + '=' + (getCurrentState(mID) == 1? 2 : 1) + '; expires=' + oneyear.toGMTString() + '; path=/';
// show or hide the menu
showMenu(mID);
}
// -->
</script>
<ul id="menu2">
<li class="header">Header</li>
<li><a href="/foo.html">» foo</a></li>
<li><a href="/bar.html">» bar</a></li>
</ul>
<script type="text/javascript">initMenu('menu2');</script>
|
I still have to add the cookie part which should take me less then an hour perhaps. ;)
simple code:-
<dt><a onclick="switchMenu('myvar');">Swtich link</a></dt>
<dd id="myvar">Content</dd>
</dl>
function switchMenu(obj) {
var el = document.getElementById(obj);
if ( el.style.display!= 'none' ) {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}
Try this one out. No ids used. It could be part of a cookie-based persistence system, if the cookie stored the index of the <dd> element in the list.
<html><head> <style type="text/css">.togList { font-family: verdana; }
.togList dt { cursor: pointer; cursor: hand; }
.togList dt span { font-family: monospace; }
.togList dd { width: 200px; padding-bottom: 15px; }
html.isJS .togList dd { display: none; }
</style> <script type="text/javascript">
/* Only set closed if JS-enabled */ document.getElementsByTagName('html')[0].className = 'isJS';
function tog(dt) { var display, dd=dt; /* get dd */ do{ dd = dd.nextSibling } while(dd.tagName!='DD'); toOpen =!dd.style.display; dd.style.display = toOpen? 'block':'' dt.getElementsByTagName('span')[0].innerHTML = toOpen? '-':'+' ; } </script>
</head><body>
<dl class="togList"> <dt onclick="tog(this)"><span>+</span> Header 1</dt> <dd> <p>Info info info info Info info info info Info info info info Info info info info</p> <p>Info info info info Info info info info Info info info info Info info info info</p> </dd> <dt onclick="tog(this)"><span>+</span> Header 2</dt> <dd> <p>Info info info info Info info info info Info info info info Info info info info</p> <p>Info info info info Info info info info Info info info info Info info info info</p> </dd> </dl></body> </html>
Next one:-
To elaborate on my previous post, here's a quick and dirty but working example. It uses a UL for the menu. The function showMenu loops through the collection of LIs and shows or hides them. The (clickable) listitem with class="header" will always be shown.
<script type="text/javascript">
<!--
var aPrefix = 'pr';
function getCurrentState(mID) {
// returnvalues: 1 (closed), 2 (opened)
return document.cookie.substr(document.cookie.indexOf(mID+'=') + mID.length +1, 1);
}
function initMenu(mID) {
// make the header clickable, assign an ID
var kids = document.getElementById(mID).childNodes;
for (var i = 0; i < kids.length; i++) {
if (kids[i].className == 'header') {
kids[i].onclick = toggleMenu;
kids[i].id = aPrefix + mID;
}
}
// show or hide the menu
showMenu(mID);
}
function showMenu(mID) {
var currentState = getCurrentState(mID);
document.getElementById(aPrefix+mID).title = (currentState == 1? 'show' : 'hide') + ' this menu';
document.getElementById(aPrefix+mID).style.cursor = 'pointer';
var kids = document.getElementById(mID).childNodes;
for (var i = 0; i < kids.length; i++) {
if (kids[i].tagName == 'LI' && kids[i].className!= 'header') {
kids[i].style.display = currentState == 1? 'none' : 'block';
}
}
}
function toggleMenu(e) {
if (window.event) e = window.event;
var mID = e.srcElement? e.srcElement.id : e.target.id;
mID = mID.substr(aPrefix.length);
// write the cookie
var oneyear = new Date(); oneyear.setFullYear(oneyear.getFullYear() + 1);
document.cookie = mID + '=' + (getCurrentState(mID) == 1? 2 : 1) + '; expires=' + oneyear.toGMTString() + '; path=/';
// show or hide the menu
showMenu(mID);
}
// -->
</script>
<ul id="menu2">
<li class="header">Header</li>
<li><a href="/foo.html">» foo</a></li>
<li><a href="/bar.html">» bar</a></li>
</ul>
<script type="text/javascript">initMenu('menu2');</script>
Comments
Post a Comment