5

All,

How to close and open a jgrowl manually

 jQuery("div.jGrowl").trigger("jGrowl.close");

The above code doesnt seem to work.

Thanks.

Hulk
  • 32,860
  • 62
  • 144
  • 215

11 Answers11

9

This worked for me

$("div.jGrowl").jGrowl("close");
Saiful
  • 1,592
  • 3
  • 15
  • 18
  • This syntax apparently doesn't work anymore. All it did for me was pop another jGrowl with a message of "close" – Kristopher Feb 15 '13 at 18:32
2

This one worked for me, closing the last notification:

$('div.jGrowl-close').triggerHandler('click');

Edit: Improved efficiency by replacing trigger with triggerHandler. See http://api.jquery.com/triggerHandler/

squarebrackets
  • 987
  • 2
  • 12
  • 19
2

I had the same issue because I was adding a set of buttons into the growl notification. One was used to accept the notification and the other was used to ignore the notification. The approach I took was to add an afterOpen callback and then add a click handler with the reference to the original close element:

afterOpen: function(e,m,o){
    $(e).find("#acceptInvitation").click(function() {
        alert( "Invite Accepted" );
    });
    $(e).find("#declineInvitation").click(function() {
        $(e).children("div.jGrowl-close").trigger('click');
    });
}
2

Three options:

$('div.jGrowl').find('div.jGrowl-notification').children().parent().remove();

$('div.jGrowl').find('div.jGrowl-notification').trigger('jGrowl.close');

$('div.jGrowl').find('.jGrowl-close').trigger('jGrowl.close');

Any of them close all notifications. I'm not sure how to close an individual notification, though.

Justin
  • 6,611
  • 3
  • 36
  • 57
Fernando
  • 129
  • 1
  • 7
1

The best method is

$(".jGrowl-notification:last-child").remove();

When jGrowl create a notification it use to create two jGrowl-notification DIVs where one is empty. So when ever you create a notification the last is the latest notification. So you need to close the last DIV with jGrowl-notification class.

When ever you wanted to open only single note use the below code, which will remove the previous note and create a new one

$(".jGrowl-notification:last-child").remove();

$.jGrowl("Hello World");
Sriansri
  • 11
  • 1
1

$(".jGrowl-notification:last-child").jGrowl('close');

or

$(".jGrowl-notification:last-child").trigger('jGrowl.close')

Are the best ways to close a notification as all of the callbacks will be triggered accordingly.

Removing the mode bypasses the callbacks, which is less then ideal. Also note that to close a specific notification you will want to use the selector noted above.

The $.fn.jGrowl() method call is designed to parallel jQuery.UI's widget approach and has become fairly common place and I will most likely maintain this in future releases.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
stanlemon
  • 399
  • 3
  • 10
1

The namespace comes after the event ..

so it should be

jQuery("div.jGrowl").trigger("close.jGrowl");

update after comment
The syntax mentioned in their blog could be a typo .. (did you try it out to see if it works ?)

Other than that, a support request at jquery plugins page dated feb 2009 may contain some more insight .. have a look ..

Igor
  • 33,276
  • 14
  • 79
  • 112
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Use the click function and catch the event to close the current notification. For example:

$.jGrowl('Hi, It's a clickable close notification', {
      click: function(e, m, o) {
        $(e).jGrowl("close");
      }
    });

Here e refers to event, m for the message and o refers to the options.

Check this for more details.

NOTE: Use the latest jgrowl lib 1.3 onward.

  • You just need to run your snippet to see the error. – Pingolin Feb 01 '19 at 08:09
  • Hi Armel, Sorry for convinience. It's just an example. Use this example(https://github.com/stanlemon/jGrowl/tree/master/examples) with my snippet. Hope it will work for you. – Akshay Kumar Feb 05 '19 at 12:15
0

Here is a solution to close individual notifications:

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

CoffeeScript and dialog here.

Mike Knoop
  • 506
  • 2
  • 11
0

Here's another solution for opening and closing individual messages that uses jGrowl's grouping feature:

var nextGroupID = 1;

var addMessage = function(message)
{
    var groupID = nextGroupID++;
    $.jGrowl(message, { sticky: true, group: "group-" + groupID });
    return groupID;
};

var removeMessage = function(groupID)
{
    $("div.jGrowl-notification.group-" + groupID).trigger("jGrowl.close");
}

// Example usage
addMessage("The first test message");
var testID = addMessage("The second test message");

setTimeout(function() {removeMessage(testID);}, 4000);
-1
$('#jGrowl').find('.jGrowl-close').trigger('click');
Mahesh
  • 567
  • 2
  • 14