[Javascript] [Beginner] What is going on with my show/hide function?

Mark Kelly javascript at wastedtimes.net
Mon Nov 13 11:27:42 CST 2006


Hello.

I have a written a simple function to toggle the visibility of a <div> that 
is, it seems to me, behaving oddly. On divs that are initially shown, the 
function works perfectly, toggling the content visibility. On divs that 
are initially hidden, the first click appears to do nothing but 2nd and 
subsequent clicks work fine. The code seems to me (as a beginner to 
javascript but not programming) to be so simple I can't imagine what I'm 
doing wrong.

I set the initial display value explicitly by setting the div class to 
either .shown or .hidden just to set the display property.

Any advice is appreciated.

Mark

===============================================
JAVASCRIPT
===============================================
function toggleDiv(divID) {
	myReference = getRefToDiv(divID);
	if(!myReference) {
	window.alert('Failed to get a reference to the named DIV');
	return;
	}
	if(myReference.style) {
		if(myReference.style.display != 'none') {
			myReference.style.display = 'none';
			} else {
			myReference.style.display = 'inline';
			}
		}
	}
===============================================
CSS
===============================================
.hidden {display : none;}
.shown {display : inline;}
===============================================
HTML 
===============================================
<a href="#" onClick="toggleDiv('nav-dir-table')" title="show/hide this 
list">Directories</a>




More information about the Javascript mailing list