• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

109
Views
js prevent blur from textarea

I am coding an editor, I have a menu tool bar, I want to keep my selection and cursor position in the textarea when I click on the toolbar. I have read most of relevant posts however none applies.

const app = document.querySelector('button');
const editor = document.querySelector('textarea');

// method 1 
/*
editor.addEventListener('blur', ()=>{
    editor.focus();
});
*/

// method 2
/*
app.addEventListener('click', e=>{
    e.preventDefault();
    e.stopImmediatePropagation();
    e.stopPropagation();
})
*/
<button>app</button>
<textarea></textarea>

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

0

From your question, you want to bring focus to text area when user clicks the button, below code provided, please let me know if this solves your issue.

const app = document.querySelector('button');
const editor = document.querySelector('textarea');

// method 1 
/*
editor.addEventListener('blur', ()=>{
    editor.focus();
});
*/

// method 2

app.addEventListener('click', e=>{
    e.preventDefault();
    e.stopImmediatePropagation();
    e.stopPropagation();
    editor.focus();
})
<button>app</button>
<textarea></textarea>

about 3 years ago · Juan Pablo Isaza Report

0

const app = document.querySelector('button');
const editor = document.querySelector('textarea');


// method 3

app.addEventListener('click', e=>{
    setTimeout(()=>editor.focus());
})
<button>app</button>
<textarea></textarea>

strong text

about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error