Skip to content

Instantly share code, notes, and snippets.

@adjohu
Last active December 15, 2015 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adjohu/5205884 to your computer and use it in GitHub Desktop.
Save adjohu/5205884 to your computer and use it in GitHub Desktop.
$(document).on("click", ".edit_trigger", function () {
var $trigger = $(this);
// Find linked editable
var editableID = $trigger.data("editable");
var $editable = $("#" + editableID);
// Replace editable with input.
var $input = $("<input>").val($editable.html().trim());
$editable
.after($input)
.hide();
// Replace button with save & cancel buttons.
var btnClasses = "btn h28 floatright";
var $save = $("<a>Save</a>")
.addClass(btnClasses)
.click(function () {
var val = $input.val();
$editable.trigger("editable:save", val);
finishEditing(val);
});
var $cancel = $("<a>Cancel</a>")
.addClass(btnClasses)
.click(function () {finishEditing()});
$trigger
.after($save, $cancel)
.hide();
// Closure to put original element back
var finishEditing = function (newVal) {
if (newVal) $editable.html(newVal);
$editable.show();
$trigger.show();
$input.remove();
$save.remove();
$cancel.remove();
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment