7

I want to trigger an event onchange of data of a table cell.

// table generating code

<table id=custorder2 > <head> <tr> <td>change it</td> </tr> </head> <tbody> <tr> <td>1</td> </tr> </tbody> </table>

// for triggering event something like this

$(document).ready(function(){
    $('#custorder2 tr td').change(function() {
         console.log("call");
    })
})

on change of cell value this call should be printed.

rrk
  • 15,677
  • 4
  • 29
  • 45
RishiPandey
  • 212
  • 1
  • 5
  • 22

4 Answers4

4

You can try to use the events like DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved or a combination of them.

$('#custorder2 tr td').on("DOMSubtreeModified", function(){
  alert('changed');
});
$('button').on('click', function(){
  $('#custorder2 tr td').text(this.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id=custorder2 > <head> <tr> <td>change it</td> </tr> </head> <tbody> <tr> <td>1</td> </tr> </tbody> </table>
<button id="id1">Change 1</button>
<button id="id1">Change2</button>
rrk
  • 15,677
  • 4
  • 29
  • 45
  • This didn't work for me, however a similar [answer](https://stackoverflow.com/a/28044851/3976696) using `$("#custorder2 ").bind("DOMSubtreeModified", function() {})` did. – sc28 Nov 29 '17 at 17:26
4

If you are considering standard edit to cell, then i would like to suggest an alternative,

<script language="javascript">
function dotest(element,event)
{
    //GET THE CONTENT as TEXT ONLY, you may use innerHTML as required
   alert("content " + element.textContent)

 }
</script>
<table cellpadding="2" cellspacing="0" border="1" width="100%"> 
  <tr>
<td contenteditable="true" onkeyup="javascript:dotest(this,event);">

    Testing
</td>
 </tr>
</table>

Relies on html5 attribute and keypress (In scenarios where cells can be directly edited)

Hope you find it useful

Hemant
  • 1,999
  • 2
  • 12
  • 8
2

You can use input listener.

$(document).on('input','#table > tbody > tr > td',function(){

akansh tayal
  • 949
  • 6
  • 3
-1

By the way on-change event does not work on tr or td, but still if you want to try you can go through the code below.

In your code i guess that target is not been recognized, so try doing:

$("#custorder > tr > td").change(function(){
});

Instead of ("#custorder tr td").change(function(){});

HTML :

<table id=custorder2 >
<head> 
   <tr> 
      <td>change it</td> 
   </tr> 
</head>          
<tbody> 
   <tr> 
      <td>1</td> 
   </tr> 
</tbody> 
</table>

Javascript :

$("#custorder > tr > td").change(function() {
     console.log("inside change event");
});
Kunal Gadhia
  • 350
  • 2
  • 14