tags:

views:

42

answers:

5

Hi All, in one of my variable i have something like var x='.myclass'. Now I want to append this var x while traversing the dom in jQuery.What should be the exact format for that.

Lets say I want to write

$("div.myclass a").someoperation();

now this .myclass has to dynamic, so I am trying something like

$("div"+x+"a").someoperation()

but this is not working, any clue.

A: 
var x = '.myclass';
$('div' + x + ' a').someoperation();

is equivalent to:

$('div.myclass a').someoperation();

so if the first doesn't work as the second it is because the value of x is not .myclass.

Darin Dimitrov
+1  A: 
$("div"+x+" a").someoperation();

Just missing a space?

ScottE
+3  A: 

I think you missed a space here:

$("div"+x+" a").someoperation()
Denis
A: 

Here is another alternative

where x is ".myclass"

$('div').find(x).find('a')
JapanPro
A: 
x='.'+var_className
$("div"+x+" a").someoperation();
Praveen Prasad