<div id="content">
<h1>Redigeringsbara stycken</h1>
<p class="editable">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
//JAVASCRIPT
window.onload = function () {
/*
var pElements = document.getElementsByTagName('*');
for (var i=0; i < pElements.length; i++) {
if(pElements[i].className == 'editable')
inPlaceEditor(pElements[i]);
};
*/
document.onclick = catchIt;
};
function inPlaceEditor (editableElement) {
}
var editing = false;
if (document.getElementById && document.createElement)
{
var butt = document.createElement('BUTTON');
var buttext = document.createTextNode('Ready!');
butt.appendChild(buttext);
butt.onclick = saveEdit;
}
function catchIt(e) {
if (editing) {
alert("U're still editing!!");
return;
}
if (!document.getElementById || !document.createElement) return;
if (!e) var obj = window.event.srcElement;
else var obj = e.target;
while (obj.nodeType != 1) {
obj = obj.parentNode;
alert(obj.nodeType);
}
if (obj.tagName == 'textarea' || obj.tagName == 'a') return;
while (obj.nodeName != 'P' && obj.nodeName != 'HTML')
{
obj = obj.parentNode;
alert(obj.nodeName);
}
if (obj.nodeName == 'HTML') {
// alert ("this is HTML");
return;
}
//other way to grab the text instead of innerHTML-method????
var targetInnerHtml = obj.innerHTML;
var textArea= document.createElement('textarea');
var parentnode = obj.parentNode;
parentnode.insertBefore(textArea, obj);
parentnode.insertBefore("<br />", obj);
parentnode.insertBefore(butt, obj);
parentnode.removeChild(obj);
textArea.value = targetInnerHtml ;
textArea.focus();
editing = true;
}
function saveEdit() {
var area = document.getElementsByTagName('textarea')[0];
var paragraph = document.createElement('P');
var parentnode = area.parentNode;
paragraph.innerHTML = area.value;
parentnode.insertBefore(paragraph, area);
parentnode.removeChild(area);
parentnode.removeChild(document.getElementsByTagName('button')[0]);
editing = false;
}
HELLO GULRS AND BOYS! Im wondering if: 1- there's another way to grab the html-text instead of using innerHtml- method? for those who work in Jquery would it be easier to implement this code in Jquery? lest lines, faster maybe? gracias! :)
contenteditable="true"will make the text content of a node and its child elements editable by the user. This works for me in Chrome, FF and IE8. See here for an example.divto be editable, you would write<div contenteditable="true">. Note that the specifics of its implementation vary from browser to browser, but the basics are fairly good cross-browser. It's actually been around since IE5.5.