// TODO: use namespacing for events (e.g. click.mycustomnamespace_toggle)
// TODO: use namespacing for data attributes (e.g. data-mycustomnamespace-xxx...)
function setupEventhandlers(){
setupSimpleEventhandlers();
setupComplexEventhandlers();
}
setupEventhandlers();
function setupSimpleEventhandlers() {
var simpleactions = ['toggle','show','hide','slideUp','slideDown'];
var simpletargets = ['target','siblings','next','prev','nextAll','prevAll','nextfirst','prevfirst'];
// create a batch of delegated event handlers for combined data attributes:
// [data-<action>-<target>]
// TODO: event namespace + .off()
$(document.body).off('.simpleactiontarget');
$.each(simpleactions, function(i1, action){
$.each(simpletargets, function(i2, target){
var data_action = action.toLowerCase();
var data_target = target.toLowerCase();
var jqfn = action;
var jqtarget = target;
var data = data_action + '-' + data_target;
var selector = '[data-' + data + ']';
var event_namespace = '';
$(document.body).on('click.simpleactiontarget', selector, function(e){
var d = $(e.currentTarget).data(data) || undefined;
if(target === 'target'){
$(d).toggle();
} else if(target === 'nextfirst'){
$(e.currentTarget).nextAll(d).first()[jqfn]();
} else if(target === 'prevfirst'){
$(e.currentTarget).prevAll(d).first()[jqfn]();
} else {
$(e.currentTarget)[jqtarget](d)[jqfn]();
}
});
});
});
}
/**
* toggle (almost) anything by setting data attributes.
* [data-toggle-relative] OR [data-toggle-selector] : start from current element, or targeted element
* [data-toggle-closest] if given, from target above find closest parent (including self) matching given value as selector.
* [data-toggle-find] if given, from target above find closest parent (including self) matching given value as selector.
*/
function setupComplexEventhandlers(){
$(document.body).on('click',...
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.