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

76
Views
Prevent scrolling to focused input/textarea element on change

I made a quick sandbox here for demonstration.

const btn = document.querySelector("button")
const topTextarea = document.querySelector("#input1")

topTextarea.addEventListener("change", (e) => {
  e.preventDefault();
  e.stopPropagation()
})
topTextarea.addEventListener("keyup", (e) => {
  // e.preventDefault();
  e.stopPropagation()
})
topTextarea.addEventListener("keydown", (e) => {
  e.preventDefault();
  e.stopPropagation()
})
btn.addEventListener("click", () => {
  topTextarea.focus({
    preventScroll: true
  });
})
body {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

textarea {
  height: 100px;
}

#demo {
  height: 3000px;
}
<textarea name="" id="input1" cols="30" rows="10">asdaasdasdsadasd</textarea>
<div id="demo"></div>
<button>Scroll Top element</button>
<textarea name="" id="" cols="30" rows="10">Bottom</textarea>

Issue: Focus on any of textarea on page. Scroll out of them(while they are focused) then type something. Default behavior happens, page scrolls to the focused element.

In the sandbox you can see a button which triggers focus but prevents scroll.

Is it possible to implement so that when on change happens, we prevent scroll ?

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

0

I found a solution.

Prevent keyDown event. Gather the entered char and update your 'state'.

And you can do some calculation and scroll to the element.

For my case scrollIntoView was causing issues with our design thats why i had to scroll manually.

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!