Search code examples
javascripthtmlhtml-tableclickable

Clickable table


I created a simple function in JS that generates a table. Now I want to do this: when I click on some cell of the table, the page will alert the value what is inside the cell: variable b, as you can see in the code. I tried it, but I didn´t managed it. is inside the code. I also used Jquery.

The code: JS

    function gen() {
        var rowData = '';
        for (var a = 0; a < 5; a++) {
            rowData += '<tr>';
            for (var i = 0; i < 3; i++) {
                
                rowData += '<td>';

                rowData += b;

                rowData += '</td>';

                b++;

            }
            rowData += "</tr>";
            
        }

        $('#myTableId2').append(rowData);


    }

HTML:

 <table id="myTableId2"> </table>
 <button onclick="gen()">Generate</button>



Solution

  • rowData += '<td onclick="alert(this.innerHTML)">';