I want to edit data in a table using jQuery.
The HTML markup is:
<tr id="unique_rid1">
<td>15/01/2012</td>
<td>4</td>
<td><a href="#" class="edit_work_done" id="unique_rid1">Edit</a></td>
</tr>
When user clicks on “Edit”, this link becomes “Update” and the table cell contaning the number 4 turns into a text field.
Here is the jQuery code for that:
//change the work done to be a form when "edit" is clicked
var toUpdate;
$(".edit_work_done").click(function(){
$(this).removeClass('edit_work_done').addClass('update_work_done').html("Update!");
toUpdate = $(this).parent().parent().children('td').eq(1);
var Hours = toUpdate.html();
toUpdate.html('<input type="number" value="'+ Hours +'" class="new_hour span1"/>');
});
//change the work done to be static value when "Update!" is clicked
$(".update_work_done").live("click", function(){
$(this).removeClass('update_work_done').addClass('edit_work_done').html("Edit");
var newHours = $(this).parent().parent().find("input.new_hour").val();
toUpdate = $(this).parent().parent().children('td').eq(1);
toUpdate.html(newHours);
});
However, “live” catches the class change immediately and does not allow the <input> tag to come up or the link to change to Update!
Question: How should I bind this event? (as a simple “click” event does not capture the dynamically updated class change)
You should give live to both