I have a .on(click) perfectly working the first time, but fails the second, in the way I describe below. This is the code:
$('.inline-edit').on('click', function() {
if (!$(this).find(':input').length) {
var current_val = $(this).find('span').html();
$(this).html('<input type="text" class="txt" maxlength="27" value="' + current_val + '"/>');
//On outside click, cancel
$(this).unbind('clickoutside').bind('clickoutside', function(event) {
cancelEdit($(this), current_val);
})
}
});
When the <td>
that has the class inline-edit are clicked, the value in the inner span is obtained, and an input is enabled. This way the user can do an inline edit. When the user clicks outside the <td>
, the edit is canceled, calling cancelEdit(), which is:
function cancelEdit(element, value) {
$(element).html('<span>' + value + '</span><img id="pencil_edit" src="/images/pencil_edit.png">');
}
The cancelEdit function returns the <td>
to its original form, which is:
<td class="inline-edit">
<span>abcdpqsdfsdfsdfrstuvwxyz<?php echo $i?></span>
<img id="pencil_edit" src="/images/pencil_edit.png"/>
</td>
The problem is that after cancelEdit is called, now when the user clicks on the <td>
, if they click over the <span>
part (the text), the .on('click')
function does not work properly, because the <span>
element is being obtained, instead of the <td>
. This however only happens the second time, even though the html is exactly the same as the first time. Why is this happening?