I'm developing an editable table feature as described in a previous post: Editable row in a table - is using the record ID on a <tr> element bad practice?
I've got the following markup:
<tr data-id="2">
<td>Foo</td>
<td><button class="editBtn">Edit</button></td>
</tr>
<tr data-id="3">
<td>Bar</td>
<td><button class="editBtn">Edit</button></td>
</tr>
Using jquery I've been able to work out which row I'm editing by targeting the .editBtn
and then using the following to work out which is the data-id
element:
$('.editBtn').click(function() {
id = ($(this).closest('tr').data('id'));
console.log(id);
});
What I don't understand is given this ID how can I then target the td
element which has my category name in it (Foo and Bar, in this example)?
I want to be able to take the content in these and place them in an editable field thus:
<td><input type="text" value="Foo"></td>
I also need to be able to change the 'Edit' button to a 'Save' button at this point, with a different class name, e.g.
<td><button class="saveBtn">Save</button></td>
How can I do this?