Prevent line/paragraph breaks in contentEditable

Daniel Cassidy picture Daniel Cassidy · Jan 8, 2009 · Viewed 42.1k times · Source

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?

Answer

kamens picture kamens · Jan 9, 2009

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.