ion
ion

Reputation: 1059

jQuery Syntax: Using a variable to target an id

I'm trying to do the following but keep failing:

I have a variable:

var targg = "new-house";

And I would like to use the value of the variable to target the element with that id:
However I'm trying to do that inside a set of parameters and it does not work.

Here is the final Code:

var targg = "new-house";
$(this).someFunction({
 para1: true,
 para2: "string",
 para3: "#"+targg
});

This however works:

$(this).someFunction({
 para1: true,
 para2: "string",
 para3: "#new-house"
});

It is probably very easy but I cannot find it!! Hope that it is clear. Thanks

Upvotes: 1

Views: 1563

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

$(this).someFunction({
 para1: true,
 para2: "string",
 para3: '#'+ targg +''
});

Upvotes: 1

x10
x10

Reputation: 3834

This

var id = '#' + targg
$(id)

targets the proper selector.

Either you don't have an element with such an id, or you aren't calling it correctly in your someFunc().

See relevant fiddle:

http://jsfiddle.net/njyXZ/5/

Upvotes: 0

Related Questions