Ryan Ryan - 9 days ago 4
jQuery Question

Is it possible to make a jquery selector take in a variable?

I have links dynamically displaying on a page, their IDs are also dynamically created.

Can I do something like this:

$('#dynamicVariable').otherStuff();


I've been trying different things like:

var whatever = "'#"+originalVariable+"'";

$(whatever).doStuff();


None of this is working. Is there a correct way to do this? Or is it just not possible?

When the user clicks a link, I want something like a check mark to display next to each link clicked. The problem as I said above, is these are all dynamically created, so I can't hard code anything in.

Answer

Use

var whatever = "#"+originalVariable;

You want whatever to be a string equal to "#foo" not "'#foo'"

The outside quotes don't actually exist ... It's just a way to tell the parser you are describing a string.

Comments