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

314
Visualizações
How to block click event on child element using parent element
    <div id="parent">
      <div>
         <button onClick={() => console.log("a")}>a</button>
      </div>
      <button onClick={() => console.log("b")}>b</button>
      <button onClick={() => console.log("c")}>c</button>
    </div>

As above, it has a parent element and a child element. In this situation, is it possible to prevent the child element from being clicked through the parent element, or to prevent the child element's onclick event from being fired?

In the above situation, console.log should not be generated no matter which button is clicked.

There is a way to cover the parent element by adding another element, but I'm curious how to prevent it through only the parent element.

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

0

Assign CSS pointer-events: none to the parent, the following will target the first <button> on the page:

document.querySelector('button').parentElement.style='pointer-events:none'

With the OP specific layout this will work for all three <button>:

document.getElementById('parent').style='pointer-events: none'

Also, the inline onclick doesn't work with normal arrow functions, if you really need to use arrow inline event, it needs to be an iife:

<button onclick="(() => console.log('b')()">

There's really no need to so, in fact inline event handlers are garbage use onevent properties or event listeners, read on events and event delegation.

The example below shows a quick way for every <button> on the page directly.

document.querySelectorAll('button').forEach(btn => btn.style='pointer-events:none');
<div id="parent">
      <div>
         <button onClick='console.log("a")'>a</button>
      </div>
      <button onClick='console.log("b")'>b</button>
      <button onClick='console.log("c")'>c</button>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

For vanilla JavaScript, there's also a way to prevent the child events from triggering even they're assigned beforehand.

You may add a click event to the parent, set useCapture by assigning the third parameter to true in the addEventListener function and then e.stopPropagation in the function body.

This will make sure this event triggers before normal event phases, so all its child click events will not trigger. It also keeps the buttons clickable and will not alter their appearance.

document.querySelector('#parent').addEventListener('click', e => e.stopPropagation(), true);
<div id="parent">
  <div>
     <button onclick="console.log('a')">a</button>
  </div>
  <button onclick="console.log('b')">b</button>
  <button onclick="console.log('c')">c</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

On the front-end, there is a library called "React". It has a "UseState" function.

const [disabled, setDisabled] = useState(false);

In this case, you can set the variable to true

<button onClick={() => setDisabled(true)}>a</button>

By clicking on the button and where you want to disable it. to place

<button disabled={ disabled } onClick={() => console.log("b")}>b</button>
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