Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onchange event of table td?

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.

like image 772
RishiPandey Avatar asked Jan 23 '16 07:01

RishiPandey


People also ask

What is the Onchange event?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed. Tip: This event is similar to the oninput event.

What triggers Onchange event?

The onchange JavaScript event is triggered when an element is changed and then loses focus. In the context of a textarea , this happens when the content of the textarea is modified and then the textarea loses focus because the user clicks away or presses the tab key.

Can I use Onchange in Div?

No; the onchange attribute is only applicable to form field elements ( input , select , textarea , etc). Thanks for your answer.


3 Answers

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

like image 147
Hemant Avatar answered Oct 24 '22 00:10

Hemant


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>
like image 30
rrk Avatar answered Oct 24 '22 00:10

rrk


You can use input listener.

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

like image 43
akansh tayal Avatar answered Oct 24 '22 01:10

akansh tayal