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

151
Visualizações
clicking on body except specific element of specific element

Here is my code:

$(document).click(function(e){
  if( $(e.target).closest("#myid").length > 0 ) {
    return false;
  }
  alert('clicked');
});
#myid{
  border: 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myid">
    something <br>
    <a target="_blank" href="#">link</a>
    <p>paragraph</p>
  </div>
</div>

As you see, when you click out of div#myid element, an alert will be shown. I want that alert also be shown when you click on the link. How can I do that?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To do this you can add another condition to your if which excludes the a element inside the #myid element. Try this:

$(document).click(function(e) {
  var $target = $(e.target);
  if ($target.closest("#myid").length > 0 && $target.is(':not(a)')) {
    return false;
  }
  alert('clicked');
});
#myid {
  border: 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myid">
    something <br>
    <a target="_blank" href="#">link</a>
    <p>paragraph</p>
  </div>
</div>

about 4 years ago · Santiago Trujillo Relatório

0

I've added another check as follows:

 if( $(e.target).closest("#myid").length > 0 && $(e.target).attr("id") !=="linkID")

Add id attribute to your link as:

<a target="_blank" id="linkID" href="#">link</a>

Please see the fiddle. Its working fine

$(document).click(function(e){
  if( $(e.target).closest("#myid").length > 0 && $(e.target).attr("id") !=="linkID") {
    return false;
  }
  alert('clicked');
});
#myid{
  border: 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myid">
    something <br>
    <a target="_blank" id="linkID" href="#">link</a>
    <p>paragraph</p>
  </div>
</div>

about 4 years ago · Santiago Trujillo Relatório

0

You need to get the current target that you clicked. In this case you need to distinguish from the current target (which will be the anchor) before anything else.

You can tell which element you clicked using the .is() jQuery method. Example:

$(document).click(function(e) {
  if($(e.target).is("a")){
    alert("You clicked the anchor");
    e.stopPropagation();
    return;
  }

  if ($(e.target).closest("#myid").length > 0) {
    return false;
  }

  alert('clicked');
});

I added a condition before proceeding to check if the clicked element is an anchor element. Better add something to select it uniquely, like a class or an Id.

I use the .is() method which returns true or false based on the condition you pass for interrogating a particular DOM element. In this case I check if the element is an anchor tag. Note, I use .stopPropagation() method to prevent the event from bubbling up, this is not needed in this particular case, but it is a good practice in such situations, as you might end up with unexpected behavior. Lastly, don't forget to return in order to avoid executing the code that follows.

If you don't need to keep the anchor's default behavior, i.e. don't want to navigate to the location designated in the href attribute, just prevent that using the event.preventDefault() method or returning false.

Check on fiddle here.

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