Is there a way to prevent the contenteditable feature from adding a div when Enter key is pressed, using pure JavaScript instead of jQuery?
I found an example that uses jQuery here: http://jsfiddle.net/uff3M/, but I specifically need a solution in plain JavaScript.
I managed to solve my issue. Thank you all for your help!
function enterToBr(e){
var evt = e || window.event;
var keyCode = evt.charCode || evt.keyCode;
if(keyCode==13){
document.execCommand('insertHTML', false, '<br>');
return false;
}
}
div{
border:1px black solid;
padding:10px;
}
<div contenteditable="true" id="container" onkeydown="enterToBr()">
When Enter is pressed, it currently creates a new div inside the container. However, I would like it to create a new line break (br element) instead.
</div>