Td element on click

Discussion in 'JavaScript' started by queen_mary, Nov 9, 2009.

  1. #1
    I have a table created using javascript. Since I am iterating the element <td> for the table I have not assigned an id to the <td> element.

    I wish to change the contents of a <td> element on click (I know how to change the innerHTML using JS if I know the id of the td element) . The action is same for all <td> elements on click.

    How would I do this?

    Please help
     
    queen_mary, Nov 9, 2009 IP
  2. TSH-Nathan

    TSH-Nathan Greenhorn

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Could you post the code that you are using to create the table, that will help :)
     
    TSH-Nathan, Nov 9, 2009 IP
  3. queen_mary

    queen_mary Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is how i create the table:

    document.write ("<body>");
    document.write("<table style='text-align: left; width:400px; height:400px' border='1' cellpadding='1' cellspacing='1' id='myTable'>");
    document.write ("<tbody>");
    for(i=0;i<10;i++) {
    document.write("<tr>");
    for(j=0;j<10;j++) {
    document.write("<td id='' onclick = 'myfunction();' >&nbsp</td>");
    }
    }
    document.write ("</tbody>");
    document.write ("</table>");
    document.write ("</body>");
    Code (markup):

    How do I generate a td id automatically (say from 1 -for cell 1 to n -for cell n) ?
     
    queen_mary, Nov 9, 2009 IP
  4. TSH-Nathan

    TSH-Nathan Greenhorn

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Something like this?

    document.write ("<body>");
    document.write("<table style='text-align: left; width:400px; height:400px' border='1' cellpadding='1' cellspacing='1' id='myTable'>");
    document.write ("<tbody>");
    for(i=0;i<10;i++) {
    document.write("<tr>");
    for(j=0;j<10;j++) {
    document.write("<td id='id-"+i+"-"+j+"' onclick = 'myfunction();' >&nbsp</td>");
    }
    }
    document.write ("</tbody>");
    document.write ("</table>");
    document.write ("</body>");
    Code (markup):
     
    TSH-Nathan, Nov 9, 2009 IP