Skip to content

Instantly share code, notes, and snippets.

@jorgepinon
Last active December 15, 2015 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgepinon/5261177 to your computer and use it in GitHub Desktop.
Save jorgepinon/5261177 to your computer and use it in GitHub Desktop.
A tiny jquery plugin that adds a close link to an alert div
(function($){
$.fn.extend({
//pass the options variable to the function
alert: function(options) {
//Set the default values
var defaults = {
pull: 'right',
callback: function () {}
}
var options = $.extend(defaults, options);
return this.each(function() {
var opts = options;
// assign current alert to a variable
var alertbox = $(this),
closeLink = '<a href="#" class="icon-close pull-' + opts.pull + '">&times;</a>';
// remove any existing close icons
$(this).find(".icon-close").remove();
$(this).prepend(closeLink).on('click', function (e) {
e.preventDefault();
$(this).closest(".alert").fadeOut(200, function () {
$(this).remove();
if (typeof defaults.callback == 'function') {
defaults.callback.call(this);
}
});
});
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment