Rataiczak24 Rataiczak24 - 24 days ago 5x
jQuery Question

Button and ContentEditable Functions

I have a table that includes both a "Edit/Save" and "Delete" button. Whenever I click the "Edit" button, it makes the rows editable and changes to a "Save" button so I can save changes. However, when I do this, it makes every cell editable and only the first row does the edit button work. It does not work for the second row, third row, etc.

My question is a two parter...

  1. How can I make certain cells in the row not editable and the others editable? I am specifically wanting the first cell "MR_ID" to not be editable.

  2. How can I get the Edit function working for multiple rows instead of only the first row?

Relevant HTML/PHP code:

foreach ($dbh->query($sql) as $rows){
<td id="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
<td id="mr_name" contenteditable="false"><?php echo $rows['MR_Name']?></td>
<td id="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
<td id="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>
<td id="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
<td id="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
<td><button id="edit" name="edit">Edit</button>
<button id="delRow" name="delete" onclick="deleteRow(this)">Delete</button></td>

Relevant Javascript code:

$(document).ready(function() {
$('#edit').click(function() {
var $this = $(this);
var tds = $this.closest('tr').find('td').filter(function() {
return $(this).find('#edit').length === 0;
if ($this.html() === 'Edit') {
tds.prop('contenteditable', true);
} else {
tds.prop('contenteditable', false);


Have a look at the jquery .not function to remove specific elements from your selection.

Your function is only working for the first row because you are using IDs on the tds. You should only have one item with a specific id on a page. Change these to classes and your code should work. You are also specifically targeting the closest row and so will only get the first.

Once you have fixed up these things your code should look something like:

var tds = $this.find('tr td').not('.mr_id').filter ....