最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onchange event of table td? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Jan 23, 2016 at 7:28 rrk 15.8k4 gold badges30 silver badges47 bronze badges asked Jan 23, 2016 at 7:18 RishiPandeyRishiPandey 2121 gold badge5 silver badges22 bronze badges 10
  • td is not an input element whose value can change? did you meant change programmatically? – gurvinder372 Commented Jan 23, 2016 at 7:20
  • yes, you get it right – RishiPandey Commented Jan 23, 2016 at 7:21
  • onChange doesn't work for td or tr – Raviteja Commented Jan 23, 2016 at 7:21
  • get value of td by document.getElementById("td_id").textContent – user0946076422 Commented Jan 23, 2016 at 7:22
  • 1 Possible duplicate of Jquery Event : Detect changes to the html/text of a div – rrk Commented Jan 23, 2016 at 7:39
 |  Show 5 more comments

4 Answers 4

Reset to default 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>

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

You can use input listener.

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

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");
});
发布评论

评论列表(0)

  1. 暂无评论