0

I am trying to add a new row on change of value of a column wherein I am using contenteditable=true for that particular column. Sample code is given below.

<td contenteditable="true" id="tdId" class="num" (change)="changeFunc($event)" (click)="clickFunc($event)">

I am able to get click event and row is getting added every time I click the my column. I have tried few possibilities for adding new row when user changes the value completely then my new row must be added but here on click of every input or pressing delete button row gets added as in click function I have passed input event.

In ts file I am writing like this:

changeFunc(event){
 console.log("in change func",event);
 }

 clickFunc(event){
  document.getElementById("tdId").addEventListener('change',function(e){
  console.log("inside event listener")    //Not working
 })

  $('table').on('change','td[contenteditable]',()=>{
   console.log("inside change")    //Not working
 })

  $('table').on('input','td[contenteditable]',()=>{
   console.log("inside input ")    //Working
    //I want this to execute only once user has edited the fields of td column and not on 
     every change of input or delete button
   var v1=document.getElementById("tdId");
   var tblBody=document.getElementById('newTable').getElementByTagName('tbody')[0]
   var newNode= tblBody.rows[0].cloneNode(true);
   tblBody.appendChild(newNode);
 })
 }

Sample working code added here - https://stackblitz.com/edit/angular-jquery-integration-ecw78c?file=app/app.component.html

After I change Editable Column Value 1 to Editable Column Value 02, I want new row to be added.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Tony
  • 19
  • 8
  • You are missing a tag, and if it is Angular, why use jQuery in the first place? – mplungjan Sep 27 '21 at 12:52
  • `(change)="changeFunc($event)" ` is not jQuery nor HTML - it looks like Angular, no? – mplungjan Sep 27 '21 at 12:55
  • 1
    Please click [edit] and then `[<>]` stack snippet and provide a [mcve] – mplungjan Sep 27 '21 at 13:00
  • Added here -https://stackblitz.com/edit/angular-jquery-integration-ecw78c?file=app/app.component.html – Tony Sep 27 '21 at 13:12
  • There are several older discussions on here saying that there is no `change` event for contenteditable. And the MDN page for the `input` event explicitly mentions contenteditable, whereas for the `change` event it talks about form fields only. – CBroe Sep 27 '21 at 13:24
  • So how should i proceed in this? – Tony Sep 27 '21 at 13:27
  • Maybe this can help you (uses input-event instead of change): https://stackoverflow.com/questions/52981372/angular-with-typescript-unexpected-behavior-in-contenteditable-td-element-in/52981988 – Gunnar B. Sep 27 '21 at 13:29
  • I am doing the same thing, just I want to add once user clicks other row or any other place . – Tony Sep 27 '21 at 13:36
  • You're doing Angular wrong. jQuery shouldn't be used to modify the DOM anyway. See https://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background. – isherwood Sep 27 '21 at 14:26

0 Answers0