Mircea
Mircea

Reputation: 11593

jQuery setting class to a variable

I am trying to set class to a variable. My code is:

var vtxt = $(this).attr("id");
$("('" + vtxt + "')").addClass("on");

It does not work this way. I've tried several things with no luck. What is the right format for adding a setting class to a variable?

Thank you

Upvotes: 2

Views: 11976

Answers (1)

brettkelly
brettkelly

Reputation: 28205

Do this:

var vtxt = $(this).attr('id');
$("#"+vtxt).addClass('on');

But, it's worth noting that (given the above example) you can just do this and get the same result:

$(this).addClass('on');

Upvotes: 9

Related Questions