Hi x[E]nOn,
No two elements can have the same ID in a document - but they can have the same class name. Experiment with the following. Feel free to ask any questions you may have about it.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set Display By Class</title>
<style type='text/css'>
.btn {
border-bottom: 1px dotted #000;
cursor: pointer;
}
.collapsible {
background: #ccc;
}
</style>
<script type='text/javascript'>
window.onload = function()
{
document.getElementById('btnOff').onclick = function() {
xGetElementsByClassName('collapsible', document, 'span', function(e){e.style.display = 'none';});
};
document.getElementById('btnOn').onclick = function() {
xGetElementsByClassName('collapsible', document, 'span', function(e){e.style.display = 'inline';});
};
}
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementsByClassName(c,p,t,f)
{
var r = new Array();
var re = new RegExp("(^|\\s)"+c+"(\\s|$)");
var e = p.getElementsByTagName(t);
// var e = xGetElementsByTagName(t,p); // See xml comments.
for (var i = 0; i < e.length; ++i) {
if (re.test(e[i].className)) {
r[r.length] = e[i];
if (f) f(e[i]);
}
}
return r;
}
</script>
</head>
<body>
<h1>Set Display By Class</h1>
<p><span id='btnOff' class='btn'>Set display:none</span> on all spans with class=='collapsible'.</p>
<p><span id='btnOn' class='btn'>Set display:inline</span> on all spans with class=='collapsible'.</p>
<h2>But I must explain...</h2>
<p>Sed ut <span class='collapsible'>perspiciatis unde omnis</span> iste natus error</p>
<h2>Sed ut perspiciatis</h2>
<p><span class='collapsible'>But I must</span> explain to you how all this mistaken</p>
<h2>On the other hand...</h2>
<p>At vero eos <span class='collapsible'>et accusamus et iusto</span> odio dignissi</p>
<h2>At vero eos et</h2>
<p>On the other hand, <span class='collapsible'>we denounce</span> with righteous i</p>
</body>
</html>
Bookmarks