Search code examples
javascriptalertify

How to catch click on alertify.js notification


I need to catch the click (or hide) event of a alertify.js event. I set up the time to 0 in order to wait to the user for click in the message. Is there any way to attach a function to this event?

<link rel="stylesheet" href="alertify.js-0.3.11/themes/alertify.core.css" />
<link rel="stylesheet" href="jalertify.js-0.3.11/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify.js-0.3.11/lib/alertify.min.js"></script>

<script>
    alertify.log('test','',0);
</script>

Solution

  • You can attach an event to the document, and see if the element clicked on has a class that matches the class names attached to alertify logs (alertify-log).

    For example, you could use code like this:

    document.body.addEventListener('click', function (e) {
      if(e.target.className.indexOf('alertify-log') > -1) {
        console.log('Clicked on a log');
      }
    }, false);
    

    Demo