Tuesday, July 1, 2008

Row Identity in HTML Tables

I found this simple script allows me to control table rows individually with Javascript in a table created dynamically through JSP.

<html>

<body>
    <script> var x = 0; </script>
    <table>
    <% for (int j=0; j<10; j++>

         <script> document.write("<tr id=tr" + x++ + 
           " onclick=\"javascript:alert(this.id);\"><td> abc </td></tr>"); 
         </script>

    <% } %>


    </table>


</body>
</html>


I save the file as index.jsp in a Tomcat webapp folder (i.e., "whatever") and then open my browser to http://localhost:8080/whatever/index.jsp to see a list of ten abc's. Each is clickable and gives a unique result.