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

185
Views
How do I check if a specific element or it's children have been/not been clicked with react.js

I'm trying to check if anything but the ordered list and it's children have been clicked here

function Heeeeelp😭() {
    function checkClick(e) {
        if (!e.target == first-list || children) { //looking for something like this
            //do something
        }
    }
    return (
        <div onClick={e => {checkClick(e)}} className="container">
            <ol className="first-list">
                <li></li>
                <li></li>
                <li></li>
            </ol>
            <ul className="second-list">
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
)}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use something like this if (e.target.closest('.second-list')) {..}, this statement, here is a complete example

const { useState } = React;

function Comp() {
    const [ clicked, setClicked ] = useState('NONE');
    function checkClick(e) {
        if (e.target.closest('.first-list')) {
          setClicked('first list');
        }
        
        if (e.target.closest('.second-list')) {
          setClicked('second list');
        }
    }
    return (
        <div onClick={e => {checkClick(e)}} className="container">
            <ol className="first-list">
                <li>first</li>
                <li>second</li>
                <li>third</li>
            </ol>
            <ul className="second-list">
                <li>first</li>
                <li>second</li>
                <li>third</li>
            </ul>
            <div>Clicked List <strong>{ clicked }</strong></div>
        </div>
)}

ReactDOM.render(
  <Comp/>,
  document.body
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

I've used this pattern a bunch and it does the trick (assuming you can use hooks). An equivalent pattern could be made without hooks if needed.

import { useRef, useEffect } from "react";

export default function App() {
  const listRef = useRef(null);

  const handleClickOutside = (e) => {
    if (listRef.current && !listRef.current.contains(e.target)) {
      console.log("Clicked somewhere besides the list.");
    }
  };

  useEffect(() => {
    document.addEventListener("mousedown", handleClickOutside, true);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside, true);
    };
  }, []);

  return (
    <div>
      <ol ref={listRef}>
        <li>List 0 Item 0</li>
        <li>List 0 Item 1</li>
        <li>List 0 Item 2</li>
      </ol>
      <ul>
        <li>List 1 Item 0</li>
        <li>List 1 Item 1</li>
        <li>List 1 Item 2</li>
      </ul>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

You can write 2 separate functions, one for each list and pass them directly to the lists instead of trying to handle everything from the container.

That way, clicking on any child of a list will trigger the correct desired behavior

const Component = () => {
  const handleClickFirstList = () => {
    // do something
  }

  const handleClickSecondList = () => {
    // do something else
  }

  return (
    <div className="container">
      <ol className="first-list" onClick={handleClickFirstList}>
        <li></li>
        <li></li>
        <li></li>
      </ol>
      <ul className="second-list" onClick={handleClickSecondList}>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
)}
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!