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

202
Visualizações
JQuery on click area - don't trigger click event when clicking specific child element?

I have an HTML block that has a little X icon in the corner. When the user clicks within this area I call a function, but I DON'T want to call this function when clicking the X that sits within that area. Here's my example simplified:

html:

<ul id="team_members">
    <li class="member">
        <span>Some text</span>
        <span>Some text</span>
        <span>Some text</span>
        <i class="icon-remove"></i>
    </li>
</ul>

jquery:

$("#team_members").on('click', ".member:not(.icon-remove)", function(e) {
    //some code
    alert("clicked!");
 });

What I am hoping will happen, is that clicking anywhere in the "member" <li> will trigger my onclick function but when I click the "icon-remove" the function will NOT be triggered, as I want a different function to trigger there.

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

0

The selector string ".member:not(.icon-remove)" will still match the .member element, so the listener will still run. Check if the clicked element - the target - to see if it's the icon or not.

$("#team_members").on('click', function(event) {
  if (event.target.matches('.icon-remove')) {
    // icon clicked
    return;
  }
  //some code
  alert("clicked!");
);
about 4 years ago · Juan Pablo Isaza Relatório

0

The issue with your code is that you are matching a .member element which is not .icon-remove where what you want is a child of .member which is not .icon-remove. So you just need a space between .member and :not(.icon-remove) in the selector:

$("#team_members").on('click', ".member :not(.icon-remove)", function(e) {
    //some code
    alert("clicked!");
 });
 
$("#team_members").on('click', ".member .icon-remove", function(e) {
    //some code
    alert("deleted!");
 });
.icon-remove:after {
  content: 'delete';
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="team_members">
    <li class="member">
        <span>Some text</span>
        <span>Some text</span>
        <span>Some text</span>
        <i class="icon-remove"></i>
    </li>
</ul>

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