Flyer53
Flyer53

Reputation: 764

Chaining unknown functions/methods in jquery

Assuming I have a jquery collection or a variable representing a jquery collection and I want to chain a jquery method to an item of the collection. For example:

$('#container').fadeIn();

So far no problem. But what if I don't know the name of the method to chain yet at the time the script is loaded because the method name is the value of an item in a configuration object?

I hope it's clear what I mean. Thank's in advance.

Upvotes: 0

Views: 82

Answers (2)

mshsayem
mshsayem

Reputation: 18008

Use Bracket Notation:

var dynamicMethodName = "fadeIn"; // from configuration
$('#container')[dynamicMethodName]();

Upvotes: 2

Amit Joki
Amit Joki

Reputation: 59232

You can always use bracket notation:

$('#container')["fadeIn"]();

So, store the function name in a variable and then use it.

var funcName = "fadeIn";
$('#container')[funcName]();

Upvotes: 0

Related Questions