Created
October 29, 2013 20:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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