Prevent line/paragraph breaks in contentEditable

JavascriptFirefoxContenteditable

Javascript Problem Overview


When using contentEditable in Firefox, is there a way to prevent the user from inserting paragraph or line breaks by pressing enter or shift+enter?

Javascript Solutions


Solution 1 - Javascript

You can attach an event handler to the keydown or keypress event for the contentEditable field and cancel the event if the keycode identifies itself as enter (or shift+enter).

This will disable enter/shift+enter completely when focus is in the contentEditable field.

If using jQuery, something like:

$("#idContentEditable").keypress(function(e){ return e.which != 13; });

...which will return false and cancel the keypress event on enter.

Solution 2 - Javascript

This is possible with Vanilla JS, with the same effort:

document.getElementById('idContentEditable').addEventListener('keypress', (evt) => {
	if (evt.which === 13) {
		evt.preventDefault();
	}
});

You should not use jQuery for the most simple things. Also, you may want to use "key" instead of "which": https://developer.mozilla.org/en-US/docs/Web/Events/keypress

Update, since keypress is deprecated:

document.getElementById('idContentEditable').addEventListener('keydown', (evt) => {
    if (evt.keyCode === 13) {
        evt.preventDefault();
    }
});

Solution 3 - Javascript

Add the following CSS rule to hide line breaks. This is only a style setting, you should add some event handlers to prevent inserting line breaks:

.your_editable br {
    display: none
}

Solution 4 - Javascript

Other than adding line breaks, the browser adds additional tags and styles (when you paste text, the browser also appends your pasted text style).

The code below covers it all.

  • When you press enter, no line breaks will be added.

  • When you paste text, all elements added by the browser are stripped from the text.

      $('[contenteditable]').on('paste', function(e) {
          //strips elements added to the editable tag when pasting
          var $self = $(this);
          setTimeout(function() {$self.html($self.text());}, 0);
      }).on('keypress', function(e) {
           //ignores enter key
       	 return e.which != 13;
      });
    

Click here for a live example

Solution 5 - Javascript

another option is to allow breaks to be entered but remove them on blur. this has the advantage of dealing with pasted content. your users will either love it or hate it (depending on your users).

function handle_blur(evt){
  var elt=evt.target; elt.innerText=elt.innerText.replace(/\n/g,' ');
}

then, in html:

<span onblur="handle_blur(event)" contenteditable>editable text</span>

Solution 6 - Javascript

If you are using JQuery framework, you can set it with the on method which will let you have the desired behavior on all elements even if this one is added lately.

$(document).on('keypress', '.YourClass', function(e){
	return e.which != 13; 
});   

Solution 7 - Javascript

Add:

display: flex;

On the contenteditable html element

Solution 8 - Javascript

$("#idContentEditable").keypress(function(e){ return e.which != 13; });

Solution proposed by Kamens doesn't work in Opera, you should attach event to document instead.

/**
 * Pass false to enable
 */
var disableEnterKey = function(){
	var disabled = false;
	
	// Keypress event doesn't get fired when assigned to element in Opera
	$(document).keypress(function(e){
		if (disabled) return e.which != 13;
	});					
	
	return function(flag){
		disabled = (flag !== undefined) ? flag : true;
	}
}();	

Solution 9 - Javascript

If you want to target all the contentEditable fields use

$('[contenteditable]').keypress(function(e){ return e.which != 13; });

Solution 10 - Javascript

I came here searching for the same answer, and was surprised to find it's a rather simple solution, using the tried and true Event.preventDefault()

const input = document.getElementById('input');

input.addEventListener('keypress', (e) => {
  if (e.which === 13) e.preventDefault();
});

<div contenteditable="true" id="input">
  You can edit me, just click on me and start typing.
  However, you can't add any line breaks by pressing enter.
</div>

Solution 11 - Javascript

Use CSS:

word-break: break-all;

For me, its worked!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel CassidyView Question on Stackoverflow
Solution 1 - JavascriptkamensView Answer on Stackoverflow
Solution 2 - JavascriptandyrandyView Answer on Stackoverflow
Solution 3 - JavascriptfrogattoView Answer on Stackoverflow
Solution 4 - JavascriptMaor OzView Answer on Stackoverflow
Solution 5 - JavascriptamwinterView Answer on Stackoverflow
Solution 6 - JavascriptPatrick DesjardinsView Answer on Stackoverflow
Solution 7 - Javascriptadir kandelView Answer on Stackoverflow
Solution 8 - Javascriptuser42507View Answer on Stackoverflow
Solution 9 - JavascriptTerrillo WallsView Answer on Stackoverflow
Solution 10 - JavascriptAnonymousSBView Answer on Stackoverflow
Solution 11 - JavascriptWoese TIView Answer on Stackoverflow