I have a function like so:
last_value = null;
$('td').click(function(e) {
console.log($(e.target).attr("id"));
if ($(e.target).is("td")) {
var the_form = $("#edit-form").detach();
if (last_value) {
$("#" + last_value.attr("id")).replaceWith(last_value);
}
last_value = $(e.target).clone();
$(the_form).removeClass("hidden");
var the_input = $(the_form).find("input.form-control");
$(the_input).attr("name", $(e.target).attr("id"));
$(the_input).attr("placeholder", $(e.target).text());
$(e.target).css('padding-top', 1);
$(e.target).css('padding-bottom', 1);
$(e.target).text("");
$(e.target).append(the_form);
}
});
This is supposed to take a table cell, and produce an inline form populated with the cell contents, which it replaces. Additionally, code has been added so that when a different cell is clicked, the contents revert to their original values. However, the problem I'm running into is this: suppose I click one cell, A. The form appears the way it should. Then suppose I click cell B. The form then "moves" to that cell, and the contents in cell A revert to their original values. Now suppose I click on cell A again. In this case, not only does the form not appear, it stays in cell B. In fact, the console.log doesn't even fire. What am I doing wrong here?
$('td).on('click', function(){ /* your code */ });