Prevent contenteditable adding <div> on ENTER - Chrome
Try this:
$('div[contenteditable]').keydown(function(e) {
// trap the return key being pressed
if (e.keyCode === 13) {
// insert 2 br tags (if only one br tag is inserted the cursor won't go to the next line)
document.execCommand('insertHTML', false, '<br/>');
// prevent the default behaviour of return key pressed
return false;
}
});
Click here for demo
prevent contenteditable adding div on enter using pure javascript NOT jQuery
function enterToBr(e){ var evt = e || window.event; var keyCode = evt.charCode || evt.keyCode; if(keyCode==13){ document.execCommand('insertHTML', false, '<br>'); }}
div{ border:1px black solid; padding:10px;display: inline-block;}
<div contenteditable="true" id="container" onkeydown="enterToBr()">When Enter it's create new div inside container , but I need when user press enter key create new br not new div</div>
Related Topics
Show Hide Divs on Click in HTML and CSS Without Jquery
How to Detect a Mobile Device With JavaScript
How to Make a Jquery "$.Post" Request Synchronous
Does Schema.Org Markup Work If Markup Is Dynamically Built With JavaScript
For Loop Through Array Only Shows Last Value
Get Element CSS Property (Width/Height) Value as It Was Set (In Percent/Em/Px/Etc)
How to Make the Browser Wait to Display the Page Until It's Fully Loaded
Jquery Counting Elements by Class - How to Implement This
Why Does Date.Parse Give Incorrect Results
Why Is Using the JavaScript Eval Function a Bad Idea
How to Display JavaScript Variables in a HTML Page Without Document.Write
Passing Form Data to Another HTML Page
Programmatically Trigger "Select File" Dialog Box
How to Update Placeholder Color Using JavaScript
Canvas2D Todataurl() Different Output on Different Browser
Detecting the System Dpi/Ppi from Js/Css