Created
January 12, 2012 09:58
-
-
Save Victa/1599653 to your computer and use it in GitHub Desktop.
Saving contenteditable Content Changes as JSON with Ajax
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
| document.addEventListener('keydown', function (event) { | |
| var esc = event.which == 27, | |
| nl = event.which == 13, | |
| el = event.target, | |
| input = el.nodeName != 'INPUT' && el.nodeName != 'TEXTAREA', | |
| data = {}; | |
| if (input) { | |
| if (esc) { | |
| // restore state | |
| document.execCommand('undo'); | |
| el.blur(); | |
| } else if (nl) { | |
| // save | |
| data[el.getAttribute('data-name')] = el.innerHTML; | |
| // we could send an ajax request to update the field | |
| /* | |
| $.ajax({ | |
| url: window.location.toString(), | |
| data: data, | |
| type: 'post' | |
| }); | |
| */ | |
| log(JSON.stringify(data)); | |
| el.blur(); | |
| event.preventDefault(); | |
| } | |
| } | |
| }, true); | |
| function log(s) { | |
| document.getElementById('debug').innerHTML = 'value changed to: ' + s; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://css-tricks.com/snippets/javascript/saving-contenteditable-content-changes-as-json-with-ajax/