0

I managed to get the first values of the dynamic table. But the code needs to get the element(values) from the table column that is clicked and edited.

//this is dynamic table
//UPDATED
$(receipts).each(function (index, item) {
        console.log(item);
        //console.log(receipts[index]);
        $('#receipts tbody').append(`<tr style="color: black;" class="trReceipts">
        <td>${item.nazivAlata}</td>
        <td oninput="timerInsert(this)" contenteditable="true" class="alKol">${item.kolicina}</td>
        <td  hidden class="idVoz">${item.idVozila}</td></tr>`)

});
//this is timer that is triggering the saveData function when value is changed in item.kolicina column
//UPDATED
function timerInsert(element){   
  
  let $element = $(element), tId = $element.data("timer"); clearTimeout(tId);   
  tId= setTimeout(function() { saveData($element) }, 1500); 
  $element.data('timer', tId);

}

//this is saveData here I need code to get the values of edited column ID and KOLICINA but the ones that are in the selected edited column/row
//UPDATE code changed to solved answer by @mplungjan
function saveData($element) { 

  const $tr = $element.closest('tr');   
  let $kol = $tr.find('.alKol'); 
  let $idVoz = $tr.find('.idVoz');
  console.log($kol.text()); 
  console.log($idVoz.text());

}

On the image below, I edited the second row "Kolicina" column, and the alert after that showed the value from the first row instead of second "Kolicina" value.

enter image description here

Tried a lot of things, from find closest to :selected but could not make it work.

NikolaN
  • 3
  • 4
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research. If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Mar 28 '23 at 08:54
  • IDs need to be unique. Use a class instead – mplungjan Mar 28 '23 at 08:55
  • @mplungjan ID-s from database are unique, but the duplicate question is not solving my problem since all my table id are the same i cant distinguish rows. – NikolaN Mar 28 '23 at 09:24
  • Hmm okay I changed the oninput but it gives me the same with "relative navigation from that element" part since I cant find out how to find oninput row/column that is changed. @mplungjan – NikolaN Mar 28 '23 at 10:07
  • 1: `$('#receipts tbody').append(\`${item.nazivAlata}${item.kolicina}${item.idVozila}\`)` – mplungjan Mar 28 '23 at 11:49
  • 2: `function timerInsert(element){ let $element = $(element), tId = $element.data(timer); clearTimeout(tId); tId= setTimeout(function() { saveData($element) }, 1500); $element.data('timer', tId);} ` – mplungjan Mar 28 '23 at 11:51
  • 3: `function saveData($element) { const $tr = $element.closest('tr'); let $kol = $tr.find('.alKol'); console.log($kol.text()) }` – mplungjan Mar 28 '23 at 11:53
  • 1
    @mplungjan thanks so much, this is the right solution for my answer. Love you! <3 – NikolaN Mar 28 '23 at 12:26

0 Answers0