Bullet-proof Jquery implementation: Capturing click interactions without fail

Hi, I’m new to this forum and JS coding in general (learner stage!).

I am using Jquery to fire events when users interact with different elements (primarily click interactions). My concern (due to ignorance probably) is that Jquery can’t/wont always work to capture the interaction so my goal is to make minimise that potential failure rate (and learn about when such occurances may happen).

I’ve already identified a few complications:

  • element being clicked on has/doesn’t have target _blank attribute so delay is necessary
  • people open link in new tab by using Ctrl+Click, right click then “Open in new tab”, clicking middle buttons for mouses with custom configuration
  • element is an email address (I’ve had the code below end up opening the link twice)

Assuming there are NO JS errors in the code, are there any other situations where the Jquery might not capture the click event? Any browser/device specific situations?

See my code below for my efforts so far

    // Clicked Social Profile
  $('.socialprofile').click(function(){

    if ($(this).attr('target').toLowerCase() == '_blank' || e.metaKey || e.ctrlKey || e.which == 2 || e.which == 3) {
      var newtab = true;
    }

    if (!newtab) {
      e.preventDefault();
      setTimeout('location.href = "' + this.href  + '"', 150);
    }

  });

Many thanks for your help!

Hi there,

Welcome to the forums :slight_smile:

If you bind a click handler to a DOM element, jQuery will always capture that event (providing the element can be clicked on).

What is it that you are trying to do exactly?