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>
Replace paragraph by br / tag in div with contenteditable
How about trapping the user keyboard event of pressing the Enter key?
Related Topics
Jquery .Show() Not Revealing a Div with Visibility of Hidden
Css3 Keyframe Animations: End and Stay on the Last Frame
How to Get the Opacity of an Element Using JavaScript
Render Closing Tag Conditionally in Order to Emulate Calendar Behavior
How to Convert X,Y Coordinates to an Angle
Adding to Script Bundle from Partial View in .Net MVC 5
How to List All CSS Variables Names/Values Pairs from Element
JavaScript Hide/Show Div on Checkbox: Checked/Unchecked
Executing JavaScript After X Seconds
How to Detect When a New Element Has Been Added to the Document in Jquery
Get the Height of an Element Minus Padding, Margin, Border Widths
How to Get the Font Size of an Element as It Was Set by CSS
How to Dynamically Change CSS Style Attribute of Div Tag
Fading Visibility of Element Using Jquery