Rajasekar
Rajasekar

Reputation: 18948

How to use a variable in place of ID in jquery

I have a div in which id is generated with index like gal1,gal2,gal3,gal4....

var clickid="gal" + index;
$('WANT TO PLACE clickid HERE').click();

I want to place the clickid variable inside the jquery selector. How to achieve it. This questions looks silly, but im a beginner eager to learn

Upvotes: 4

Views: 642

Answers (2)

Sarfraz
Sarfraz

Reputation: 382656

Like this:

$('#' + clickid).click();

Since it is an id, you need to prepend # to it so that jQuery is able to find it out. This is similar to CSS :)

For more info, have a look at:

Upvotes: 3

Alex Reitbort
Alex Reitbort

Reputation: 13696

$("#" + clickid).click();

Upvotes: 7

Related Questions