HTML Code:
<ul id="categories">
<li><a href="#recommended">Recommended</a></li>
<li><a href="#maintenance">System maintenance</a></li>
<li><a href="#settings">Systems & settings</a></li>
<li><a href="#image">Image-related</a></li>
<li><a href="#media">Media</a></li>
<li><a href="#internet">Internet & HTML</a></li>
<li><a href="#add-ons">IE add-ons</a></li>
<li><a href="#personal-additions">Personal additions</a></li>
<li><a href="#pdf">PDF creation</a></li>
</ul>
Styled with the following CSS:
Code:
@media screen {
#categories {
display: block;
list-style-type: none;
margin: 0 0 1em;
text-align: center;
}
#categories li {
display: inline;
margin: 0;
padding: 0 0.25ex;
}
#categories a,
#categories a:visited {
background: transparent;
color: blue;
text-decoration: underline;
}
}
Each of tables should be given the relevant id attribute above. For example,
HTML Code:
<table id="recommended">
Do not use the style attribute to hide the tables.
Code:
(function() {
var active = {category : null, table : null};
function showCategory() {
var element;
if((element = active.category)) {
element.style.fontWeight = '';
}
if(this.style) {
(active.category = this).style.fontWeight = 'bold';
}
if(active.table) {
active.table.style.display = 'none';
}
if('string' == typeof this.href) {
element = document.getElementById(this.href.substring(this.href.indexOf('#') + 1));
if(element && element.style) {
(active.table = element).style.display = '';
}
}
}
function finalise() {
active.category = active.table
= null;
}
if(document.getElementById) {
window.onload = function() {
var list = document.getElementById('categories'),
element, links;
if(list && list.getElementsByTagName) {
links = list.getElementsByTagName('a');
for(var i = 0, n = links.length, c; i < n; ++i) {
c = links[i];
c.onclick = showCategory;
if('string' == typeof c.href) {
element = document.getElementById(c.href.substring(c.href.indexOf('#') + 1));
if(element && element.style) {
element.style.display = 'none';
}
}
}
}
window.onunload = finalise;
};
}
})();
Place that in a script element. It doesn't really matter where.
This has been superficially tested, and I haven't found any problems. It works equally well in IE5.01+, Firefox, Mozilla 1.3+ and Opera 7.03+. It should also degrade to something usable in all other browsers.

Originally Posted by
robertcathles
Is it possible, instead of saying document.all one after another, if I could use an array() or getelementsbyid() action to get a list of these elements at once
You could use a loop and construct the property names at run-time, but the solution above is far superior.
please note, this page works best in IE6. Firefox is rubbish at determining a javascript like the one above (it probably needs some rare extension).
No, I'm afraid it's just that you don't know what you're doing.
In the code you posted, the this operator refers to the span element. No elements have an officially defined document property, so Firefox, and most other browsers, will error out. Similarly, the all collection is a proprietary feature of IE and many browsers do not support it. That aside, the all collection in this case should be deprecated in favour of the document.getElementById method.
There are other problems, too. A table element does not have a display property value of inline. In IE, it would (incorrectly) be block, and in modern browsers it would table. In addition, if a user visits that page with scripting disabled, it is useless because all of the content is hidden. In the solution above, the content will only be hidden if it could be made visible again. The use of proper links allows users to be taken to the relevant table, which makes life far easier for them.
Finally, I wouldn't make items bold to show they are active as it makes the text shift. This happens in the both the original and the suggestion above.
Mike
Bookmarks