Nays wrote:
My code in full is:
function dsp(loc){
if(document.getElementById){
var foc=loc.firstChild;
foc=loc.firstChild;
You're setting foc twice here; is that a typo? And, you're assuming,
without checking, that foc refers to an IMG element.
if(foc.src.indexOf('down.gif')>0){
foc.src = 'up.gif';}
else{
foc.src = 'down.gif';}
foc=loc.parentNode.nextSibling.style?
loc.parentNode.nextSibling:
loc.parentNode.nextSibling.nextSibling;
foc.style.display=foc.style.display=='block'?'none ':'block';}}
This is called like so:
<div class="header"><a href="javascript:void(0)" class="dsphead"
onclick="dsp(this)">
<img src="down.gif" border="0"></img>Test</a></div>
Notice that there's a bit of (whitespace) text between the end of the
div tag and the beginning of the img tag. *That" is a Text node, and
is the firstChild of the div element whose reference is passed to
the function dsp. IE often eliminates these.
[Omitting comments about the javascript
: protocol; I'm sure others will
do so.]
A less fragile construction might be (untested)
function dsp(imgID,divID) {
var img = document.getElementById(imgID),
div = document.getElementById(divID);
if (img != null) {
if (img.src.indexOf('down.gif') >= 0) img.src = 'up.gif';
else img.src = 'down.gif';
}
if (div != null && div.style != null)
div.style.display = (div.style.display == '' ? 'none' : '');
}
and
<div class="header"><a href="#here" name="here" class="dsphead"
onclick="dsp('downImg','cntDiv');return false"><img id="downImg"
src="down.gif" border="0">Test</a></div>
<div id="cntDiv">Content - Yay!</div>
hj