Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Prevent contenteditable="plaintext-only" div from creating a new line

I have a div that is used as user input:
<div id="chat-message-input" contenteditable="plaintext-only">

When the user hits the enter key (and is not holding the shift key), the text from the user is submitted.

<script>
    document.querySelector('#chat-message-input').onkeyup = function(e) {

       // User hits enter key and is not holding shift
       if (e.keyCode === 13 && event.shiftKey != 1) {

        //Click submit button
        document.querySelector('#chat-message-submit').click();

        // Clear input field
        document.getElementById('chat-message-input').innerHTML='';
    };
</script>

Problem: There is a brief moment where the div creates a new line because the enter key was pressed, before the content in the div is cleared.

I only want a new line to be created under these circumstances:

  1. The user's input text reaches the end of the line
  2. The user holds shift and presses enter

Does anyone know how I can prevent a new line from being created given the above requirements?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As suggested by A Haworth, I also intercepted onkeydown (in addition to onkeyup) and prevented the default. This seemed to work well.

    document.querySelector('#chat-message-input').onkeydown = function(e) {
       // User hits enter key and is not holding shift
       if (e.keyCode === 13 && event.shiftKey != 1) {
            e.preventDefault()
        }
    };

This made the input field really solid, which is necessary for my real-time chat app. Thanks!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!