0

I have a table in CRUD loaded from database, I want table rows to be editable by converting table cell to input field preserving cell text as input value. I am using jQuery for that.

So far, I can add input field inside <td> tag. But data from all the cells becomes value of each input instead of becoming value of their respective input fields.

Here is my code...

$("#parEdit").click(function() {
    $(this).text("Update");
    $(this).closest('tr').children().wrapInner('<input type="text" value = ' + $('td').text() + '></input>');
})

My table looks like this...

ID Name Position Marks Edit
1 Ahmad 6th 525 Edit Button

After pressing the Edit button, the output is like this...

ID Name Position Marks Edit
1Ahmad6th525 1Ahmad6th525 1Ahmad6th525 1Ahmad6th525 Update Button

1 Answer 1

1

You need apply the wrap to each td, your original code inserted every td text.

$("#parEdit").click(function(){
   $(this).text("Update");
   $(this).closest('tr').children().each(function(){
      $(this).wrapInner('<input type="text" value = ' + $(this).text() + '></input>');
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

You have missing closing tags for the click function. Thanks for the help

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.