Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created May 13, 2013 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/5566885 to your computer and use it in GitHub Desktop.
Save joseanpg/5566885 to your computer and use it in GitHub Desktop.
EditInPlace.js
var editor = null;
var sleeping = null;
var sleepingParent = null;
function handler(ev) {
var el = ev.target;
if (sleeping) {
sleeping.innerHTML = editor.value;
editor.value = '';
editor.defaultValue = editor.value;
sleepingParent.replaceChild(sleeping,editor);
}
sleeping = el;
sleepingParent = el.parentNode;
editor.value = sleeping.innerHTML;
editor.defaultValue = editor.value;
el.parentNode.replaceChild(editor,el);
editor.focus();
ev.preventDefault();
ev.stopPropagation();
return false;
}
function editorpress() {
sleeping.innerHTML = editor.value;
//setTimeout(function(){sleeping.innerHTML = editor.value;},0);
}
function handlerout() {
if (sleeping) {
sleeping.innerHTML = editor.value;
editor.value = '';
editor.defaultValue = editor.value;
sleepingParent.replaceChild(sleeping,editor);
sleeping = sleepingParent = null;
}
}
window.onload = function() {
editor = document.createElement('TEXTAREA');
editor.id = 'editor';
editor.onmouseout = handlerout;
var els = Array.prototype.slice.call(document.querySelectorAll('.editable'));
els.forEach(function(it){
it.onclick = handler;
});
editor.onkeyup = editorpress;
}
window.onclick = handlerout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment