MIKE KNOOP

Founder, Web Developer, and Mechanical Engineer

Manually Closing Individual jGrowl Notifications

December 7, 2011

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);

ALL POSTS BACK TO HOME @ CONTACT ME SUBSCRIBE
Find more of my content across the web.
Twitter and Google+ aren't bad places to stay up to date, either!
I'm currently working on Zapier an online tool to automate tasks between hundreds of web apps.