John Davidson

javascript - Edit table row with jquery and twig


I have table in my twig template generated dynamically:

{% for a in abc %}
{{ a.key }}
{{ a.value }}
<td class="td-actions">
<button id='EditSave' data-text="Save" data-id="{{ }}">Edit</button>
<button id='Cancel' data-text="Cancel" style="display:none;">Cancel</button>
{% endfor %}

I made my js function that will change attribute on click.

I am new in js and what I want to accomplish further is that on specific row 'Edit' click these two field become editable.

I know how to implement ajax but don't know how to access is with twig and jquery.

My func:


//getting id of specific column
var id = $(this).data('id');

var btnText = $(this).text();
if(btnText === 'Edit')


My Answer:

{% for item in abc %}

{% endfor %}
Name Age Action
{{ }} {{ item.age }}

{% endfor %}
To edit a table row using jQuery, you can add a click event listener to the "Edit" button and then update the row with input fields for editing. Here's an example of how you can achieve this:
$(document).ready(function() {
$('.edit-btn').click(function() {
var row = $(this).closest('tr');
var name = row.find('td:eq(0)').text();
var age = row.find('td:eq(1)').text();
$(document).on('click', '.save-btn', function() {
var row = $(this).closest('tr');
var name = row.find('input:eq(0)').val();
var age = row.find('input:eq(1)').val();
row.html('' + name + '' + age + '');

In this code snippet, when the "Edit" button is clicked, the table row is replaced with input fields containing the current values. When the "Save" button is clicked, the input values are retrieved and used to update the table row.

Rate this post

4 of 5 based on 4907 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us