All,
How to close and open a jgrowl manually
jQuery("div.jGrowl").trigger("jGrowl.close");
The above code doesnt seem to work.
Thanks.
All,
How to close and open a jgrowl manually
jQuery("div.jGrowl").trigger("jGrowl.close");
The above code doesnt seem to work.
Thanks.
This worked for me
$("div.jGrowl").jGrowl("close");
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/
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');
});
}
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.
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");
$(".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.
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 ..
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.
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);
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);