Skip to content

Instantly share code, notes, and snippets.

@tom--
Created October 29, 2013 20:51
Show Gist options
  • Select an option

  • Save tom--/7222380 to your computer and use it in GitHub Desktop.

Select an option

Save tom--/7222380 to your computer and use it in GitHub Desktop.
jQuery plugin makes DOM elements with contenteditable attribute trigger a change event when their contents change.
/**
* 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.tomEditable = 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.trigger('change');
}
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment