Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

66
Views
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?

8 months ago · Santiago Trujillo
3 answers
Answer question

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>

8 months ago · Santiago Trujillo Report

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>

8 months ago · Santiago Trujillo Report

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.

8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs