[Javascript] A loop for this script?

Roland Dong rdong at advance.net
Fri Jul 1 13:44:36 CDT 2005


Paul:

Thanks for your reply. Maybe I did not address the problem clearly.
Basically, this is what I want. I want user a for loop for the following
code to avoid hard coding:

=====================================
var TIMER;
startList = function() {
	navBar = document.getElementById("nav").rows[0].cells;
	navBar[0].onmouseover=function() {
	
document.getElementById("tutorials").style.visibility="visible"
	}	

	navBar[0].onmouseout=function() {
	
TIMER=setTimeout('document.getElementById("tutorials").style.visibility="hid
den"', 500);
	}
		
	document.getElementById("tutorials").onmouseover=function(){
		    clearTimeout(TIMER);
	
document.getElementById("tutorials").style.visibility="visible";
	}
		
	document.getElementById("tutorials").onmouseout=function(){
	
TIMER=setTimeout('document.getElementById("tutorials").style.visibility="hid
den"', 500);
	}	  

	navBar[1].onmouseover=function() {
	
document.getElementById("scriptings").style.visibility="visible"
	}
	navBar[1].onmouseout=function() {
	
TIMER=setTimeout('document.getElementById("scriptings").style.visibility="hi
dden"', 500);
	}
		
	document.getElementById("scriptings").onmouseover=function(){
			clearTimeout(TIMER);
	
document.getElementById("scriptings").style.visibility="visible";
	}
		
	document.getElementById("scriptings").onmouseout=function(){
	
TIMER=setTimeout('document.getElementById("scriptings").style.visibility="hi
dden"', 500);
	}
		
	navBar[2].onmouseover=function() {
	
document.getElementById("validation").style.visibility="visible"
	}
		
	navBar[2].onmouseout=function() {
	
TIMER=setTimeout('document.getElementById("validation").style.visibility="hi
dden"', 500);
	}
		
	document.getElementById("validation").onmouseover=function(){
		    clearTimeout(TIMER);
	
document.getElementById("validation").style.visibility="visible";
	}
		
	document.getElementById("validation").onmouseout=function(){
	
TIMER=setTimeout('document.getElementById("validation").style.visibility="hi
dden"', 500);
	}

}

window.onload=startList;
=====================================



This is what I have tried:
======================================
	var catNames = new Array("tutorials", "scripting", "validation");
	navBar = document.getElementById("nav").rows[0].cells;
	
    for (x in navBar){

		navBar[x].onmouseover=function(){
	
document.getElementById(catNames[this.cellIndex]).style.visibility="visible"
;
			
		}

		navBar[x].onmouseout=function(){
   		    TIMER=setTimeout('document.getElementById("'
+catNames[this.cellIndex]+ '").style.visibility="hidden"', 500);
			
		}
		
	    document.getElementById(catNames[x]).onmouseover=function(){
		    clearTimeout(TIMER);
			document.getElementById().visibility="visible";

		}
		
		document.getElementById(catNames[x]).onmouseout=function(){
	
TIMER=setTimeout('document.getElementById(catNames[x]).style.visibility="hid
den"', 500);
		}
	}	


This portion of the code works:
=========================================================================
document.getElementById(catNames[this.cellIndex]).style.visibility="visible"
;
			
		}

		navBar[x].onmouseout=function(){
   		    TIMER=setTimeout('document.getElementById("'
+catNames[this.cellIndex]+ '").style.visibility="hidden"', 500);
===========================================================================




This portion of the code DOES not work!!!!!! No matter what I do I can not
pass any value to document.getElementById().
=========================================================================
    document.getElementById(catNames[x]).onmouseover=function(){
		    clearTimeout(TIMER);
	
document.getElementById(catNames[x]).).visibility="visible";

		}
		
		document.getElementById(catNames[x]).onmouseout=function(){
	
TIMER=setTimeout('document.getElementById(catNames[x]).style.visibility="hid
den"', 500);
		}
	}	
=========================================================================


Any idea?







-----Original Message-----
From: javascript-bounces at LaTech.edu [mailto:javascript-bounces at LaTech.edu]
On Behalf Of Paul Novitski
Sent: Friday, July 01, 2005 12:43 PM
To: JavaScript List
Subject: RE: [Javascript] A loop for this script?

At 08:34 AM 7/1/2005, Roland Dong wrote:
>Sorry, my mistake, I should say I tried document.getElementById("'
>+ids[x]+'") and document.getElementById(ids[x]) and none of them works. Why
>can't I get the value from the array?

Roland,

Your use of quotation marks is very strange.  Anything inside quotation 
marks is processed as a string and not evaluated [unless you use the eval() 
function which I recommend you approach very warily].  I recommend you 
re-read javacript syntax documentation such as:
http://www.croczilla.com/~alex/reference/javascript_guide/

Are you trying to force javascript to interpret ids[x] as a string?  If so, 
perhaps try this:

         document.getElementById("" + ids[x] + "")

You can probably get to the root of the problem simply by displaying the 
current value of ids[x] on each iteration.  Insert this into your loop:

         alert(x + ": ids[x] = " + ids[x]);

Without studying your code, my guess is that either ids[x] is not a string 
but rather an object or a numeric, or that the subscript x takes the array 
past the last member that has a valid value for your purposes.

Good luck,
Paul 


_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list