Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript Tables

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to figure out how to solve this problem. I need to create a table of various rows and columns--the rows and columns is entered by the user. I have to do this w/ a while loop, but I can't figure out how to make it universal enough to where every table will display correctly. I have to do up to a 20x20 table, so listing out all the possibilities of tables w/ all the code isn't an option. Also, once I get those tables to render correctly on the browswer no matter how many rows or columns are entered, I have to use the random function to make the color of the table. I understand how to make random numbers using the Math.random function, but the colors of tables can also have a,b,c,d,e,f as options also. My teacher says "use a loop to generate the 6 random hexidecimal digits for each table cell background color. HELP!!
 
I have a color calculator at If you look at the source code you'll find functions that convert decimal numbers to hexedecimal numbers and vice versa. Feel free to use any of that code.

Here is a function that returns a table, but with no content in the cells:
Code:
function table(rows, cols)
{
var t = &quot;<table>\n&quot;;
for (i=0;i<rows;i++)
  {
  t += &quot;<tr>\n&quot;;
  for (x=0;x<cols;x++)
    {
    t += &quot;<td> </td>\n&quot;;
    }
  t += &quot;</tr>\n&quot;;
  }
t += &quot;</table>&quot;;
return t;
}
You can use it like:
Code:
document.write(table(20,20));
-Petey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top