You can try to work around this IE behavior by catching ENTER and adding BR
instead of <P> </P>
.
pad.addEventListener('keydown',keyDown,false); function keyDown(e){ if(e.keyCode!=13) return; pad.innerHTML+='<br />\b'; e.stopPropagation(); e.preventDefault(); return false; } <div id="pad" contenteditable="true"></div>
The trick is to add "\ b" (backspace) to the end of the line, otherwise IE will not make a new line before the user clicks on another key. However, these feedbacks cause problems when using innerHTML
later. To remove backspaces, you simply replace them with ""
on RegExp:
padsInnerHTML=pad.innerHTML.replace(/LITERAL_BACKSPACE_CHARACTER/g,'');
In a regular expression, you will need a literal backward character, /\b/
will not work. To see the reverse character, you can run your code in Opera using this keyDown()
, write a few letters and press ENTER in the pad
and copy-paste the character into your code.
Please note that this code should only work in IE, other browsers will be corrupted with new characters.
Why this works, I do not know. IE seems to add some non-print character after <BR />
, which needs to be removed to create an instant new line after pressing ENTER.
Teemu source share