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

247
Views
How to make cursor always stay on textarea even if user clicks anywhere else on the page?

I'm developing a Chat application using Angular & Electron.

I want user's cursor always on textarea where user types the message. When user clicks anywhere else in the page, the cursor should not go away from text area. This helps user to type anytime from anywhere, it will type in textarea only.

I tried the following:

    setTimeout(() => {
      this.messageTextAreaInput.nativeElement.focus();
    }, 20);

I'm calling it on the textarea blur event. But this solution does not let me copy text from other messages. When I select any text from message history, my selection is lost.

Any other way to achieve this?

My app for reference: enter image description here

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

0

window.addEventListener("keydown", () => {document.getElementById("textarea").focus()});
<textarea id="textarea" ></textarea>

about 4 years ago · Juan Pablo Isaza Report

0

Try this way. You can change focus element to your own element which can be focusable.

const focusOnTextInput = () => document.getElementById('input').focus();

document.addEventListener('click', focusOnTextInput);
document.addEventListener('DOMContentLoaded', focusOnTextInput);
<input type="text" id="input"/>

about 4 years ago · Juan Pablo Isaza Report

0

You could try using something like

window.onclick = function(event) {
    if (!event.target.matches('.messageTextAreaInputClassName')) {
        document.getElementById('messageTextAreaInput').focus();
    }
}

So that clicking anywhere other than the text area will call the text area to focus.

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!