Reputation: 673
My code uses a jQuery plugin that inserts some HTML code.
I have the option to change the inserting method, by setting them via variable.
The variable is called:
var options = {insert: 'append'};
I don't want to do that with an if
condition, like that:
if(options.insert === 'append') {
$('#foobar').append(htmlcode);
}else if (options.insert === 'prepend'){
$('#foobar').prepend(htmlcode);
... and so on.
Is there any solution to insert the html code with a given varibale method?
Upvotes: 1
Views: 41
Reputation: 337560
Because the jQuery construct is an object, you can use bracket notation to access functions. Try this:
$('#foobar')[options.insert](htmlcode);
So long as options.insert
matches the name of a function, it'll work.
Upvotes: 6