for loop not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mark roldan
    New Member
    • Sep 2010
    • 3

    #1

    for loop not working

    Code:
    			function startrun()
    				{
    					var n;
    					var m;
    					var p;
    					var q;
    					for(q=1; q<12; q++)
    						{
    							document.write(q);
    							n="dcode"+q;
    							m="acode"+q;
    							if(document.getElementById(n).name==data[1]) && (document.getElementById(m).name==data[2])
    								{
    									p="d"+q;
    									document.getElementById(p).style.visibility="visible";
    								}
    							else
    								{
    									p="d"+q;
    									document.getElementById(p).style.visibility="hidden";
    								}
    						}
    				}
    So this is the code. I used the document.write to check if its incrementing, and i only get a value of 1. Removing the if statement makes it work... any idea? thanks..

    Other info:
    > It is an onload to the body.
    > the acode[x] and dcode[x] are anchor objects.
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Hi there,

    1) Remove/comment out the document.write. document.write forces a reload of the page and would clear your html. Use alert() instead for debugging because it doesn't cause a reload.

    2) The brackets on your if statement are misplaced. You can have if(expression && expression) or if ((expression)&& (expression)) but not if(expression) && (expression)
    Code:
    //You have
    if(document.getElementById(n).name==data[1]) && (document.getElementById(m).name==data[2])
    
    //Should look like this
    if((document.getElementById(n).name==data[1]) && (document.getElementById(m).name==data[2]))
    
    //Or this
    if(document.getElementById(n).name==data[1] && document.getElementById(m).name==data[2])

    Comment

    Working...