Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda