Reputation: 21430
I have the following code:
$('.helloWorld').on('click', function() {
$(this).css('background', 'black');
});
Instead, I would like to do something like this (for research):
var params = [
'click',
function () {
$(this).css('background', 'black');
}
];
$('.helloWorld').on(params);
I get an error that reads, "Uncaught SyntaxError: Unexpected token , ."
Is this possible? If so, how?
Upvotes: 1
Views: 65
Reputation: 9597
You are using an array in your example. Use an object instead ({}
instead of []
) Something like this should work:
var params = {
"event" : 'click',
"callback" : function () {
$(this).css('background', 'black');
}
};
$('.helloWorld').on(params.event, params.callback);
Upvotes: 3