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

294
Views
Automatically scroll a div not the parent element

I have a scrollable list of items on a page. The user is able to make a PUT request and update one of the items. If the PUT request is successful I make another network request to fetch the updated list. From this list I want to immediately set the recently updated element as active.

I have the code to do all of this successfully, however the scrolling not only occurs on my list but the entire page scrolls somewhat as well.

My list looks like this:

<div className={cx(styles.body)}>
  <div className={styles.squadList}>
    {squadList.map((squad, index) => {
       const isSelected =
         squad.squadId === squadSelected.squadId
         ? "selected"
         : null;
       return (
          <div
            className={cx(styles.item, {
             [styles["item--selected"]]: isSelected,
            })}
            key={index}
            id={`squad-list-item-${squad.squadId}`}
          >
           <div className={styles.details}>
               <span className={styles.title}>
                   {squad.squadName}
               </span>
               <span className={styles.members}>
                    {squad.memberCount} members &nbsp;
                    {renderAdminIcon(squad, isSelected)}
               </span>
           </div>
         </div>
       );
    })}
  </div>
</div>

After the successful put request and my refetch I run

const tgtElement = document.getElementById(
    `squad-list-item-${squadSelected.squadId}`
);
tgtElement.scrollIntoView();

This works and it scrolls the desired element to the top of the list, however it also scrolls the entire page a bit as well.

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

0

in order to achieve that you must scroll the div to the desired element.

First get the element offsetTop and than set the div top scroll to match the element top offset.

const divToScroll = document.getElementById("divToScrollId");
const tgtElement = document.getElementById(
    `squad-list-item-${squadSelected.squadId}`
);

// Here where the magic happens
divToScroll.scrollTop = tgtElement.offsetTop;

This should work as long as the div to scroll has is css position set to relative.

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!