Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created June 4, 2013 19:22
Show Gist options
  • Save jtallant/5708718 to your computer and use it in GitHub Desktop.
Save jtallant/5708718 to your computer and use it in GitHub Desktop.
How can I remove the duplication and do something like document.on(click, selector1, selector2, function() { // some code here });
$('.reward-sections ul li a').click(function() {
var type = $(this).data('type');
if ('link' === type || 'coming_soon' === type) {
return true;
}
var reward_data = {
'reward_id': $(this).data('reward-id'),
'status': $(this).data('status'),
'type': type
};
showRewardModal(reward_data);
});
$(document).on('click', '.resource-content li a', function(){
var type = $(this).data('type');
if ('link' === type || 'coming_soon' === type) {
return true;
}
var reward_data = {
'reward_id': $(this).data('reward-id'),
'status': $(this).data('status'),
'type': type
};
showRewardModal(reward_data);
});
@siffring
Copy link

siffring commented Jun 4, 2013

What about something like this?

function doSomething(target) {

    var type = $(target).data('type');

    if ('link' === type || 'coming_soon' === type) {
        return true;
    }

    var reward_data = {
        'reward_id': $(taret).data('reward-id'),
        'status': $(target).data('status'),
        'type': type
    };

    showRewardModal(reward_data);

}
$('.reward-sections ul li a').click(function() {
    doSomething(this);
});

$(document).on('click', '.resource-content li a', function(){
    doSomething(this);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment