The answer, written by Brian Allo, does not take into account that you need to place the cursor at the end of the div. Otherwise, with each replacement action, the user will have to press CTRL-End.
This solution that I propose can also be seen in action http://jsfiddle.net/82dS6/ :
function setEndOfContenteditable(contentEditableElement){ // Taken from http://stackoverflow.com/a/3866442/1601088 var range,selection; if(document.createRange){//Firefox, Chrome, Opera, Safari, IE 9+ range = document.createRange(); range.selectNodeContents(contentEditableElement); range.collapse(false); selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } else if(document.selection){//IE 8 and lower range = document.body.createTextRange(); range.moveToElementText(contentEditableElement); range.collapse(false); range.select(); } } function replaceDivWithP(el){ $(el).find('div').each(function(){ $(this).replaceWith($('<p>' + $(this).html() + '</p>')); }); } $(function(){ $(".text").simpleEdit(); }); $('.textarea').bind('keyup', function(){ replaceDivWithP(this); setEndOfContenteditable(this); }); β
maurits
source share