3

I have some code that displays the JSON and allows the user to edit the text. After editing, I want to allow the user to click a button to save the new input value. Everything works as expected except for grabbing that new input value.

for (let i = 0; i < jsonObject.results.length; i++) {
    var row = `<tr scope="row" class="test-row-${jsonObject.results[i].id}">
    <td id="fName-${jsonObject.results[i].id}" data-testid="${jsonObject.results[i].id}">${jsonObject.results[i].firstName}</td>
    // some code

    $(`#save-${jsonObject.results[i].id}`).click(function(){
      clickAButton(jsonObject.results[i].id, jsonObject, i);
    });

    $(`#fName-${jsonObject.results[i].id}`).on('click', editResult)
    
}
function editResult(){
  var testid = $(this).data('testid')
  var value = $(this).html()

  $(this).unbind()
  $(this).html(`<input class="result form-control" data-testid="${testid}" type="text" value="${value}">`)
}
function clickAButton() {
  var text = $(`#fName-${jsonObject.results[index].id}`).val();
  console.log("text from " + text);
  // code
}

the code above displays

text from

How do I get it to display the new user input?

5
  • Hi, how does clickAButton() gets called ? Commented Feb 13, 2021 at 4:59
  • @Swati i added the code. It's in the same original for loop Commented Feb 13, 2021 at 5:07
  • Are you able to access jsonObject and index variables inside clickButton function? Commented Feb 13, 2021 at 5:18
  • @KiranMahale yes, I am able to access jsonObject and index. I'm worried that it is no longer that id due to unbinding? Commented Feb 13, 2021 at 5:23
  • As I understood you want to take the text between the td tag so it can be accessed by textContent. Like var text = $(#fName-${jsonObject.results[index].id}).textContent; Commented Feb 13, 2021 at 5:35

1 Answer 1

1

Instead of writing mutliple event handler for all tds and button you can use only one event handler for button and td . So, when td is clicked just remove data-testid attribute from td so that again that event will not get called and to get input value use $(this).closest('tr').find('.result').val() this will give you input value where save button is clicked.

Demo Code :

var jsonObject = {
  "results": [{
    "id": 1,
    "firstName": "sas"
  }, {
    "id": 2,
    "firstName": "cd"
  }]
}

for (let i = 0; i < jsonObject.results.length; i++) {
  var row = `<tr scope="row" class="test-row-${jsonObject.results[i].id}">
    <td id="fName-${jsonObject.results[i].id}" data-testid="${jsonObject.results[i].id}">${jsonObject.results[i].firstName}</td><td><input type='button' id='save-${jsonObject.results[i].id}' value ='save'></td></tr>`

  $("table").append(row)
}

$(document).on('click', 'td[data-testid]', function() {
  var testid = $(this).data('testid')
  var value = $(this).html()
  $(this).html(`<input class="result form-control" data-testid = "${testid}" type = "text"
  value = "${value}" >`)
  //removed data-testid
  $(this).removeAttr("data-testid");
})

$(document).on('click', '[id*=save-]', function() {
  //use class to find input
  var text = $(this).closest('tr').find('.result').val();
  console.log("text from " + text);

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
</table>

Sign up to request clarification or add additional context in comments.

2 Comments

how would this work if I had another field like last name? and want to grab everything in that line?
can you edit your question with complete code ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.