I have a contenteditable
element, and whenever I type some stuff and hit ENTER
it creates a new <div>
and places the new line text in there. I don't like this one little bit.
Is it possible to prevent this from happening or at least just replace it with a <br>
?
Here is demo http://jsfiddle.net/jDvau/
Note: This is not an issue in firefox.
This is browser directed HTML5 editor. You can wrap your text with
<p>...</p>
, then whenever you press ENTER you get<p></p>
. Also, the editor works this way that whenever you press SHIFT+ENTER it inserts<br />
.Check this: http://jsfiddle.net/ZQztJ/
Try this
Demo Here
First we need to capture every key user enter to see if enter is pressed then we prevent the
<div>
creation and we create our own<br>
tag.There's one problem, when we create it our cursor stay at the same position so we use Selection API to place our cursor at the end.
Don't forget to add a
<br>
tag at the end of your text because if you don't the first enter won't do a new line.Fiddle
Prevent New Div creation in content editable Div on each enter key: Solution That I have find is very simple:
This --- innerHTML content prevent New Div creation in content editable Div on each enter key.
It overrides the default behavior to have paragraph instead.
On chrome the default behavior on enter is:
With that command it is gonna be
Now that it's more linear across it's easy to have only
<br>
would you need to.Where
this
is the actual context which meansdocument.getElementById('test');
.