ASM said:
it was so beurk :-(
"beurk"?
use of innerHTML
Actually, innerHTML is supported in more browsers than DOM methods.
It's not the way to go for purity, and maybe not for future
compatability (although I doubt we'll ever see another HTML browser
that supports DOM and not innerHTML), but currently, it works.
It's overkill for this case, though, since there is no markup in the
"HTML" that is assigned.
What's wrong with div's?
<script type="text/javascript">
function expand(elem) {
var v = elem.firstChild.nodeValue;
elem.firstChild.nodeValue = v=='[+]'? '[-]':'[+]';
elem.parentNode.lastChild.style.visibility=v=='[+]'?'visible':'hidden';
Have you tested this in, e.g., Mozilla? You are expecting the last
child node of the "p" element to be the "span" element. More likely,
it will be a text node containing the newline between "</span>" and
}
</script>
<p>Start of text
<a href="#" onclick="expand(this); return false;">[+]</a><br>
Why use a link, especially one with a non-sensical href like "#"
(something to be avioded generally). The link will make no sense
if javascript is disabled. For affordability to clicking, I would
prefer a button, not a link, since it doesn't link to anything.
(A good hint that one is misusing links is that they have no
meaningful; href
Using a link with an onclick also fails quite visibly if the script
errors, as the href is followed then (because you never reach the
"return false"). That is why it's better to use an object with no
inherent behavior, instead of trying to override it.
Using a link *does* make sense, if it links to a page with the
entire text visible. Then it would be a fall-back for javascript
disabled browsers.
<span style="visibility:hidden">More text</span>
You use "visibility" to hide the content. While it works in more
browsers than using "display" (e.g., Netscape 4 and Opera 6 and
other static DOM browsers), it also means that the hidden content
still takes up space in the flow of the page, which makes hiding
it pretty irrelevant.
So, my suggestion would be something like:
---
<script type="text/javascript">
function getElement(id) {
return document.getElementById ?
document.getElementById(id) :
document.all ?
document.all[id] :
null; // no need to fall back on Netscape here
}
function writeMoreButton(id) {
document.write("<input class='moreButton' type='button' value='+'",
" onclick='toggleMore(this,\"", id, "\");'>");
}
function toggleContent(id, visible) {
var elem = getElement(id);
(elem.style||elem).display = visible?"":"none";
}
function toggleMore(button,id) {
var expand = (button.value=="+");
toggleContent(id, expand);
button.value = expand ? "-" : "+";
}
</script>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
<span id="loremMore">Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</span>
<script type="text/javascript">
writeMoreButton("loremMore");
toggleContent("loremMore", false);
</script>
</p>
---
You have to hide the element after it occurs in the file, but the
button can be placed anywhere you want.
/L
--
Lasse Reichstein Nielsen - (e-mail address removed)
DHTML Death Colors:
<URL:
http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'