Skip to content

Instantly share code, notes, and snippets.

@Victa
Created January 12, 2012 09:58
Show Gist options
  • Select an option

  • Save Victa/1599653 to your computer and use it in GitHub Desktop.

Select an option

Save Victa/1599653 to your computer and use it in GitHub Desktop.
Saving contenteditable Content Changes as JSON with Ajax
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;
}
@Victa

Victa commented Jan 12, 2012

Copy link
Copy Markdown
Author

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