← home

Manually Closing Individual jGrowl Notifications

jGrowl is a nice little library to show simple JavaScript notifications. Unfortunately, by default, notifications can only be closed manually, on a timer, or never. There are a few solutions on StackOverflow to close all notifications but not individual notifications. For example, a “loading…” notification which closes when the action is complete.

Fortunately, there is a simple method to close individual notifications manually. Check out this CoffeeScript:

testGrowl = ->
 close = (e) ->
  console.log 'close'
  $(e).find('.jGrowl-close').trigger('click')
 ao = (e) ->
  setTimeout(
   -> close(e)
   1500)
 $.jGrowl("Hello world!", { sticky: true, afterOpen: ao })
 setTimeout testGrowl, 100
 setTimeout testGrowl, 1000

Or translating into pure JavaScript:

testGrowl = function() {
 var ao, close;
 close = function(e) {
  console.log('close');
  return $(e).find('.jGrowl-close').trigger('click');
 };
 ao = function(e) {
   return setTimeout(function() {
   return close(e);
  }, 1500);
 };
 return $.jGrowl("Hello world!", {
   sticky: true,
   afterOpen: ao
  });
 };
setTimeout(testGrowl, 100);
setTimeout(testGrowl, 1000);