Help with small Javascript issue

Discussion in 'JavaScript' started by DFORMS, Jul 21, 2010.

  1. #1
    Hi,

    Trying to create a HTML table using Javascript. Cant use the DOM or anything elaborate. Actualy the # of TR and TD have to be specified using parametres inside a function. This is what i have so far :
    
    <html>
    	<head>
    		<title>ex2</title>
    		<script language="javascript">
    			function tbl(){
    				
    				var td = document.write("<td>");
    				var tr = document.write("<tr>")
    				
    				var close_td = document.write("</td>")
    				var close_tr = document.write("</tr>")
    				      document.open();
    					  document.write("<table border='1'>");
    					
    					for(i = 0; i < 10; i++){
    						
    					document.write(tr);
    					
    					  for(j = 0; j < 10; j++){
    					  	
    					  document.write(td);
    					  document.write("AAA");
    					  document.write(close_td);
    
    					}
    					document.write(close_tr);
    				}
    				document.write("</table>");
    				document.close();
    			}
    		</script>
    	</head>
    	<body>
    		<script language="javascript">
    			tbl();
    		</script>
    	</body>
    </html>
    
    Code (markup):
    But its not working : s Can someone explain to me what is it am doing wrong?

    I made this one, but its not what my teacher wants :
    
    <html>
    	<head>
    		<title>ex2</title>
    		<script language="javascript">
    			function tbl(){
    				document.write("<table border='1'>");
    				
    				for(i = 0; i < 10; i++){
    					document.write("<tr>");
    					
    					for(j = 0; j <12; j++){
    						document.write("<td>");
    						document.write("row : " + i + " / " + "col : " + j);
    						document.write("</td>");
    					}
    					document.write("</tr>");
    				}
    				document.write("</table>");
    			}
    		</script>
    	</head>
    	<body>
    		<script language="javascript">
    			tbl();
    		</script>
    	</body>
    </html>
    
    
    Code (markup):
    Thank you for support!
     
    DFORMS, Jul 21, 2010 IP
  2. DFORMS

    DFORMS Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Sorry for double post. Here is where am at right now and looking more like what i want :
    
    <html>
    	<head>
    		<title>ex2</title>
    		<script language="javascript">
    			function tbl(nb_tr, nb_td){
    			
    				var i = 0;			
    
    				document.open();
    				document.write("<table border='1'>");
    				
    				while (i < nb_tr) {
    				
    					for (j = 0; j < i; j++) {
    						document.write("<tr>");
    						
    						while (i < nb_td) {
    						
    							for (k = 0; k < i; k++) {
    								document.write("<td>");
    								document.write("AAA");
    								document.write("</td>");
    							}
    							document.write("</tr>");
    						}
    						document.write("</table>");
    						document.close();
    					}
    					i++
    					
    				}
    			}
    
    
    
    
    		</script>
    	</head>
    	<body>
    	<script language="javascript">
            skynet_table = tbl(6, 10);
    
            document.write(skynet_table);
    	</script>
    	</body>
    </html>
    
    Code (markup):
    This code is causing problems with my navigator (Firefox), but its almost working : )
     
    DFORMS, Jul 21, 2010 IP
  3. DFORMS

    DFORMS Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I got it to work nvm.
     
    DFORMS, Jul 21, 2010 IP