0

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.

3
  • $('#container')["fadeIn"]() Commented Apr 18, 2014 at 5:27
  • it's unclear to me.... Commented Apr 18, 2014 at 5:30
  • The bracket notation is the answer I was looking for. Thank you very much :-) Commented Apr 18, 2014 at 5:58

2 Answers 2

2

Use Bracket Notation:

var dynamicMethodName = "fadeIn"; // from configuration
$('#container')[dynamicMethodName]();
Sign up to request clarification or add additional context in comments.

Comments

0

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]();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.