2

What's the simplest way to call a string method with ?

1
  • 2
    FWIW: You are not converting anything. There are just two ways to access properties of objects: Dot notation and bracket notation. The bracket notation expects a string. Commented Jul 14, 2011 at 11:51

1 Answer 1

5

If I understand your question correctly, you want to call a method whose name is stored in a string.

If that's the case, you should use square bracket notation instead of (not in addition to) dot notation, and delimit the literal string with quotes:

$["STRINGVALUE"]();

You can also use the variable you defined initially, without quotes:

$[myFunction]();
Sign up to request clarification or add additional context in comments.

4 Comments

May this error be outdated? I've tried it, but it doesn't seem to work. Have gotten it working with eval(STRINGVALUE+'()').
@user, it looks like your problem is not the same. You want to call a function named STRINGVALUE, but cept0 wants to invoke a jQuery method named STRINGVALUE.
A function being a user defined function, while a jQuery method is a function defined within the $. namespace?
@user, more or less, yes. Technically, $.fn also comes into play for jQuery methods, but in this particular case the questioner indeed wants to call a method exposed by the $ object directly (such methods are akin to static methods in other languages).

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.