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
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();' > </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) ?
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();' > </td>"); } } document.write ("</tbody>"); document.write ("</table>"); document.write ("</body>"); Code (markup):