javascriptjqueryhtmlcsscontenteditable

contenteditable single-line input


For an application we're developing at the company where I work, we need an input that supports inserting emoticons inside our JS-based web app. We're currently using an input with the emoticon shortcodes (ie ':-)') and would like to switch to inserting actual, graphical images.

Our original plan was to use a contenteditable <div>. We're using listeners for the paste event as well as the different key/mouse interactions to ensure no unwanted markup enters the contenteditable (we strip text out of its container tags and leave only image tags that we inserted ourselves).

However, the problem right now is that the div resizes if you put in enough content (ie its height increases). We don't want this to happen, nor is it acceptable for the text to just be hidden (ie plain overflow: hidden). So:

Is there a way to make the contenteditable div behave like a single-line input?

I'd like it best if there is a relatively simple attribute/css property that I've missed that will do what I want, but if necessary CSS+JS suggestions will also be appreciated.


Solution

  • [contenteditable="true"].single-line {
        white-space: nowrap;
        width:200px;
        overflow: hidden;
    } 
    [contenteditable="true"].single-line br {
        display:none;
    
    }
    [contenteditable="true"].single-line * {
        display:inline;
        white-space:nowrap;
    }
    <div contenteditable="true" class="single-line">
        This should work.
    </div>​