Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
Selecting parent container id on Click

I was wondering if it be possible to always select the parent's Id when clicking anywhere on the entire div?

I've made a sample example giving each child divs a different id and console.logging which element id is being selected depending on which part of the entire div you click on.

As said above I just want to be able to always select the parent div's Id, which in this case is "1" no matter which part of the entire div I click on.

function App() {
  function onClick(e) {
    console.log(e.target.id);
  }

  return (
    <div id="1" className="container" onClick={onClick}>
      Decor
      <div id="2" className="img-container">
        <img
          id="3"
          src="https://images.pexels.com/photos/7828323/pexels-photo-7828323.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
          className="img"
          alt=""
        />
      </div>
      <p id="4">Lorem ipsum</p>
    </div>
  );
}

ReactDOM.render(
  <App />,
  document.getElementById("root")
);
body {
  font-family: sans-serif;
  text-align: center;
  display: flex;
  justify-content: center;
}

.container {
  height: 300px;
  width: 200px;
  border: solid green;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.img-container {
  width: 200px;
  height: 200px;
}

img {
  width: 100%;
  height: 100%;
}
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>

<div id="root"></div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Search for the closest ancestor with the "container" class.

function Example() {

  function onClick(e) {
    const { id } = e.target.closest('.container');
    console.log(id);
  }

  return (
    <div id="1" className="container" onClick={onClick}>
      Decor
      <div id="2" className="img-container">
        <img
          id="3"
          src="https://images.pexels.com/photos/7828323/pexels-photo-7828323.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
          className="img"
          alt=""
        />
      </div>
      <p id="4">Lorem ipsum</p>
    </div>
  );
}

ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda