0

One of my ajax popup is loading too late.so my condition of jquery to check visibility is not working.

$(document).ready(function() {

    if($('#emailCart').is(':visible')){
        alert('yes');
        let shouldFire = true;
        $("input, select").click(function(){
         if(shouldFire) {
            alert('sent');
            sendGAEvent('Email', 'click','Email Cart');
            shouldFire = false;
           }
        });
    };
});

seems "is(':visible')" only checks for dom loaded elements.How can i apply this conditions to future elements also.

Email cart image

Email cart image

When clicking on this Email cart button many textboxes appear on clicking any one of those my code should work. I am using a tool tempormonkey by which i inject my code to websites.But my code is not working when i inject using tempormonkey but instead works with console.

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

Do it the other way around: check the visibility in the handler function.

$(document).ready(function() {
  $("input, select").click(function() {
    if ($('#emailCart').is(':visible')) {
      alert('sent');
      sendGAEvent('Email', 'click', 'Email Cart');
    }
  });
});

If the input and select elements are loaded dynamically, use event delegation as described in Event binding on dynamically created elements?. But that doesn't change the logic of how to check for visibility of the cart.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Its not possible to write such code which will execute in future but we can monitor that on click of document because you are saying that on click of Email Cart button you want to execute it.

I hope it will resolve your issue, try it:-

$(document).on('click', function (e) {
    if (!$('#emailCart').is(':visible')) return;

    alert('yes');
    let shouldFire = true;

    $("input, select").click(() => {
        if (!shouldFire) return;
        alert('sent');
        sendGAEvent('Email', 'click', 'Email Cart');
        shouldFire = false;
    });
});
Deepak Dixit
  • 1,510
  • 15
  • 24