2

I am adding rows to an existing table using JavaScript insertRow method For one cell, I want to add an onclick event.

How can I do that using pure JavaScript?

I am attaching my code.

<!DOCTYPE html>
<html>
    <head>
        <style>
        table, td {
          border: 1px solid black;
        }
        </style>
    </head>
    <body>    
        <table id="myTable">
          <tr>
            <td>Row1 cell1</td>
            <td>Row1 cell2</td>
          </tr>

        </table>
        <br>
        <button onclick="myFunction()">Try it</button>
        <script>
        function myFunction() {
          var table = document.getElementById("myTable");
          var row = table.insertRow(0);
          var cell1 = row.insertCell(0);
          var cell2 = row.insertCell(1);
          cell1.innerHTML = "NEW CELL1";
          cell2.innerHTML = "NEW CELL2";
          cell1.onclick()="xfunc()";
        }

        function xfunc(){
        alert("Hi")
        }
        </script>
  </body>
</html>
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Mohan C
  • 73
  • 1
  • 9
  • You can find your solution at [link](https://stackoverflow.com/questions/1207939/adding-an-onclick-event-to-a-table-row) – Deepak Kumar Dec 28 '18 at 07:23

5 Answers5

2

onclick is a html property, you have assign a function to this property to handler click event.

In you case:

cell1.onclick = xfunc; // instead of cell1.onclick()="xfunc()";
hoangdv
  • 15,138
  • 4
  • 27
  • 48
0

Update your function as below

   <script>
        function myFunction() {
          var table = document.getElementById("myTable");
          var row = table.insertRow(0);
          var cell1 = row.insertCell(0);
          var cell2 = row.insertCell(1);
          cell1.innerHTML = "NEW CELL1";
          cell2.innerHTML = "NEW CELL2";
          cell1.onclick=xfunc;
        }

        function xfunc(){
        alert("Hi")
        }
        </script>
Abhishek
  • 972
  • 3
  • 12
  • 24
0

I think the problem is that, How you are attaching the event to cell1? Try the following code:

function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = "NEW CELL1";
    cell2.innerHTML = "NEW CELL2";
    cell1.onclick = xfunc; //<-- problem was on this line
}
fiza khan
  • 1,280
  • 13
  • 24
Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
0

Use the below one.

cell1.addEventListener('click', xfunc);
Vamsi
  • 102
  • 9
0

First you could try something really simple:

cell1.setAttribute('onclick', 'xfunc()');

Maybe this already Handles your problem. If not you just could use a DIV inside the cell:

var div = document.createElement('div');
div.setAttribute('onclick', 'xfunc()');
div.setAttribute('style', 'height: 100%; width: 100%');
div.innerHTML = 'NEW CELL1';
cell1.appendChild(div);

This should handle the problem for sure.

The only thing im not really sure about is wether a table-cell is capable of onclick attributes :)

LG

Sandro Schaurer
  • 416
  • 4
  • 13