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

178
Views
Detect when user has scrolled through a text to enable a button

How to implement a "scroll detection" feature using React? I would like to present the user with a div with a given message, and enable a checkbox only once the user has scrolled through it. This is my current implementation, which is not working.

function Disclaimer({ text, checkboxText }) {
  const divRef = useRef();
  const [userHasReviewed, setUserHasReviewed] = useState(false);

  const scrollTracker = () => {
    if (divRef.current) {
      const { scrollTop, scrollHeight, clientHeight } =
        divRef.current;
      if (scrollTop + clientHeight === scrollHeight) {
        setUserHasReviewed(true);
      }
    }
  };

  return (
    <div>
      <div
        ref={divRef}
        onScroll={() => scrollTracker()}
      >
        {text}
      </div>
      <div>
        <Checkbox
          label={checkboxText}
          disabled={!userHasReviewed}
        />
      </div>
    </div>
  );
}

I've placed debug points inside of the scrollTracker function, but the debugger never enters the function. I think this is because the text is not large enough to actually overflow and require the scrolling space/action. In this case, how can I automatically detect this so that the userHasReviewed is set to true automatically?

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I would use the css hover selector to enable the button

almost 4 years ago · Santiago Trujillo Report

0

To detect when a user has scrolled through text and enable a button accordingly on a web page, you can use JavaScript/jQuery. Here is an example of how to achieve this:

 <!DOCTYPE html>
<html>
<head>
 <title>Detención de Desplazamiento de Texto</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
 <div id="content">
 <p>Este es un texto largo. Desplázate hacia abajo para habilitar el botón.</p>
 </div>
 <button id="myButton" disabled>Botón Deshabilitado</button>
 <script src="script.js"></script>
</body>
</html>
 $(document).ready(function() {
 const $button = $("#myButton");
 const $content = $("#content");

 let usuarioSeDesplazo = false;

 // Detecta el evento de desplazamiento en el contenido
 $content.on("scroll", function() {
 usuarioSeDesplazo = true;
 // Habilita el botón si el usuario se ha desplazado
 $button.prop("disabled", false);
 });
});


This code uses jQuery to detect the scroll event on the #content element. When the user scrolls through the content, the button with the id myButton is enabled. Make sure you include jQuery in your page before running this code.


almost 4 years ago · Santiago Trujillo 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!