Last active
September 28, 2015 09:08
-
-
Save McKabue/df4ae6598d771fc8c2a2 to your computer and use it in GitHub Desktop.
jQuery plugin to dynamically register events on dynamically added items ondemand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
$.fn.EventHandler = function (options) { | |
var defaults = $.extend({}, options), $element, $eventsArray = [], $activityArray = []; | |
$element = $(this); | |
if (options.onEvent.length != 0) { | |
if(options.startEvent){ | |
$element.on(options.startEvent, function(){ | |
$eventsArray = options.onEvent; | |
$eventsArray.forEach(function (oneEvent) { | |
$(options.listen).on(oneEvent.event, function () { | |
$activityArray = oneEvent.activity; | |
$activityArray.forEach(function (activityArrayItem) { | |
$(options.target)[activityArrayItem.action](activityArrayItem.actionValue) | |
}); | |
}); | |
}); | |
}); | |
} | |
} | |
} | |
}(jQuery)) | |
//AND USE IT LIKE THIS | |
$('.gqtrigger1').EventHandler({ | |
startEvent: 'click', | |
listen: '#gq1', | |
target: '#col1', | |
onEvent: [ | |
{ | |
event: 'shown.bs.collapse', | |
activity: [ | |
{ | |
action: 'removeClass', | |
actionValue: 'fa-plus' | |
}, | |
{ | |
action: 'addClass', | |
actionValue: 'fa-minus' | |
} | |
] | |
}, | |
{ | |
event: 'hidden.bs.collapse', | |
activity: [ | |
{ | |
action: 'removeClass', | |
actionValue: 'fa-minus' | |
}, | |
{ | |
action: 'addClass', | |
actionValue: 'fa-plus' | |
} | |
] | |
} | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment