2

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

1 Answer 1

9

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');
Sign up to request clarification or add additional context in comments.

1 Comment

$("#"+vtxt).addClass('on'); This does it. Thanx a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.