Pergunta

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.

Foi útil?

Solução

Use Bracket Notation:

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

Outras dicas

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]();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top