The following code makes a div tag editable and handles the save and cancel buttons. But there is a lot of duplication. How could I remove the duplication on this?
var originalTexts = {};
$("div.edit-panel").find("span").click(function() {
var panel = $(this).closest('div.simple-panel');
var text = panel.find(".info");
var id = $(this).parent().attr("id");
panel.find(".edit-panel").addClass('editable');
text.addClass("content-editable").prop('contenteditable', 'true');
originalTexts[id] = text.text();
})
$("div.edit-panel").find(".cancel-editable").click(function() {
var panel = $(this).closest('div.simple-panel');
var text = panel.find(".info");
var id = $(this).parent().attr("id");
text.text(originalTexts[id]);
panel.find(".edit-panel").removeClass('editable');
text.removeClass("content-editable").prop('contenteditable', 'false');
})
$("div.edit-panel").find(".save-editable").click(function() {
var panel = $(this).closest('div.simple-panel');
var text = panel.find(".info");
var currentText = text.text();
text.text(currentText);
panel.find(".edit-panel").removeClass('editable');
text.removeClass("content-editable").prop('contenteditable', 'false');
})