5

There exist some concept like variables variable to print variable names or call functions dynamically:

http://php.net/manual/en/language.variables.variable.php

Thanks in advance.

2
  • 2
    Jquery isn't a language, it's a library. Javascript (EcmaScript) is the language. Commented Sep 16, 2010 at 2:05
  • This question is similar to: Is there a JavaScript equivalent of PHP’s “variable variables”?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 10, 2024 at 10:06

2 Answers 2

7

The closest JavaScript equivalent is bracket notation, for example:

var obj = { myMethod: function() { alert("Hello!"); } };
var func = "myMethod";
obj[func](); //equal to obj.myMethod();

You can test it out here, in JavaScript calling these two is equivalent:

object.property
object["property"];

And the latter allows you to use a variable, to get any property or method you want.

To be clear this is a JavaScript behavior, there's nothing specific to jQuery about it.

Sign up to request clarification or add additional context in comments.

Comments

6

In javascript you can use a similar aproach.

$a = "hello";
$['hello'] = 'world';
$[$a];
alert($a + " " + $[$a]); // alerts "hello world"

See in jsfiddle.

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.