Skip to content

Instantly share code, notes, and snippets.

@tom--
Created November 3, 2012 12:15
Show Gist options
  • Save tom--/4007222 to your computer and use it in GitHub Desktop.
Save tom--/4007222 to your computer and use it in GitHub Desktop.
jquery plugin to make contenteditable text trigger a change event when it changes
(function ($) {
'use strict';
/**
* Makes contenteditable elements within a container generate change events.
*
* When you do, e.g. $obj.editable(), all the DOM elements with attribute contenteditable
* that are children of the DOM element $obj will trigger a change event when their
* contents is edited and changed.
*
* See: http://html5demos.com/contenteditable
*
* @return {*}
*/
$.fn.editable = function () {
this.on('focus', '[contenteditable]', function () {
var $this = $(this);
$this.data('beforeContentEdit', $this.html());
});
this.on('blur', '[contenteditable]', function () {
var $this = $(this);
if ($this.data('beforeContentEdit') !== $this.html()) {
$this.removeData('beforeContentEdit').trigger('change');
}
});
return this;
};
}(jQuery));
@tom--
Copy link
Author

tom-- commented Nov 3, 2012

it was adapted from an idea i found on so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment