11

I'd like to use a JQuery selector to return a variable by passing a text string of the variable name:

ex.

var test = "xxx";

I'm thinking I could do something like this, but it won't work:

  alert($("#test"));

Edit: People are getting confused. Think of it as a reflective way to get the contents of var test. Preferrably with a JQuery selector, but I'll take any way one can come up with it..

5
  • 3
    and alert(test) won't work because...? Commented Apr 12, 2011 at 19:53
  • because I want to use a string of the variable name. not possible? Commented Apr 12, 2011 at 19:54
  • I think you want to use that string to build a selector, correct? Commented Apr 12, 2011 at 19:55
  • I answered based on the understanding that you want the string "test" from the variable test. Am I correct? Commented Apr 12, 2011 at 20:05
  • I answered wrong. I have a string name "test", and I wanted to return the variable of that name. Commented Apr 12, 2011 at 20:07

7 Answers 7

16

This should work for you. You have to use the variable outside the string quotes like this. You could replace the # with a . if you want to use a style selector vs an ID selector.

var test = "xxx";
alert($("#" + test));

If the field is something like <input name="xxx" .../>, you would have to use something like the following:

var test = "xxx";
alert($("[name=" + test + "]"));
Sign up to request clarification or add additional context in comments.

Comments

2

Not sure what your end target is, but I use the following method a lot when I need one thing to interact with another...

<a href='whatever.html' rel='#targetElem' class='loadable'>
<script>
$(document).ready(function(){
  $('.loadable').live('click',function(){  //when a element with the class of loadable is clicked
    $($(this).attr('rel')).load($(this).attr('href')); //load the contents of it's href URL into the element specified in it's rel attribute
    return(false); // stop processing the link;
 });
});

</script>

I tell the element what the target of the action will be using the rel attribute...

to get at variables try this (window is a javascript built-in array of global variables...)

var i=2;

var test='i';
document.write('got ' + window[test]);

2 Comments

I changed your bottom suggestion to get it. Awesome! alert(window["test"]); works for my example above (IE8, FF, Chrome)
+1: innovative use of the window array. but dont recommend that anywhere :)
1

Not sure, but maybe you are looking for eval function.

Comments

1

What you are asking is best done with JavaScript objects, e.g.:

var obj = { test: "xxx" };
for(key in obj) {
    alert(key);  // "test"  
    alert(obj.key); // "xxx"  
}

You can do something like this, though I really don't see the point:

var obj = { foo: "xxx" };
for(key in obj) {
    alert($("#" + key).length); // 1   
}

<input id="foo"/>

http://jsfiddle.net/J4hC8/

1 Comment

helpful. yes this is an alternate, but I'm trying to do it without an array
1

I know this is old, but here is the solution. As stated by igor milla, you should use eval().

var test = "xxx";
alert(eval("test"));

Comments

-1

Maybe what you need is

alert($("#" + test));

1 Comment

That would get him the DOM element with the ID of the string held by test, but not the variable equivalent to the string held by test. he's trying to do a variable-variable as in echo $$test
-1
var test = "#xxx";
alert($(test));

Alternatively, you can use:

var test = "xxx";
alert($("#" + test));

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.