<h1>
<a href="http://benalman.com/news/2010/03/jquery-special-events/" target="_top">jQuery special events</a>
» Custom destroy event
</h1>
<p class="intro">(widgets must be closed in order)</p>
<div class="widget">
<div class="widget-head">
<h2>First widget</h2>
<a href="#" class="close">X</a>
</div>
<div class="widget-body">
<p>
This example illustrates one way in which the jQuery special event <code>_default</code> method can be used.
</p>
</div>
</div>
<div class="widget">
<div class="widget-head">
<h2>Second widget</h2>
<a href="#" class="close">X</a>
</div>
<div class="widget-body">
<p>
When our special 'destroy' event is triggered, if the event's default action is not prevented, the element it was triggered on will be removed from the DOM.
</p>
</div>
</div>
<div class="widget">
<div class="widget-head">
<h2>Third widget</h2>
<a href="#" class="close">X</a>
</div>
<div class="widget-body">
<p>
While the framework code determines what happens when the event is triggered, the implementation-specific code supplies the logic for deciding when that default behavior should be prevented.
</p>
</div>
</div>
<div class="widget">
<div class="widget-head">
<h2>Fourth widget</h2>
<a href="#" class="close">X</a>
</div>
<div class="widget-body">
<p>
This approach allows framework and plugin developers to further abstract the internals of their events from end-users, which allows special events to behave more like native events, and can result in more organized code!
</p>
</div>
</div>
//////////////////////////////////////////////////
// THE FOLLOWING CODE IS TAKEN FROM THE EXAMPLE //
//////////////////////////////////////////////////
// The framework code
(function($){
// When the 'destroy' event is triggered on an element, that element will
// be removed from the DOM, unless prevented with event.preventDefault().
$.event.special.destroy = {
_default: function(event){
$(event.target).remove();
}
};
})(jQuery);
// The implementation-specific code
$(function(){
// Handle the 'destroy' event for each widget.
$('.widget').bind( 'destroy', function(event){
// Only allow the widget to be destroyed if it isn't preceded by any
// other widgets.
if ( $(this).prevAll('.widget').length ) {
// Note that the 'heavy lifting' of removing the element is done
// in the framework, thus abstracting away the internals from the
// user. All they need to do is prevent it, or not.
event.preventDefault();
// An implementation-specific error message.
alert( 'Widgets must be closed in order!' );
}
});
// When an 'X' close link is clicked, trigger the 'destroy' event for
// that widget.
$('.widget .close').bind( 'click', function(event){
$(this).closest('.widget').trigger('destroy');
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.