Reputation: 129
var myValue = "sItem-" + val;
$('#something').attr('class','myValue');
The DOM show me myValue instead of the value that I set, which I expect to see sItem-1
. I console.log the myValue, but it can shows sItem-1? hmm...
I also tried $('#something').attr('class', "sItem-" + val);
I doesn't work
Upvotes: 0
Views: 49
Reputation: 10896
try something like this
var myValue = "sItem-" + val;
$('#something').addClass('class',myValue);
Upvotes: 0
Reputation: 425
Use addClass
instead of attr
: http://api.jquery.com/addClass/
And you uses myValue
with single quotes, that's wrong:
$('#something').addClass(myValue);
Upvotes: 1
Reputation: 2562
You are enclosing the variable into single quotes.
Try this
$('#something').attr('class',myValue);
instead of
$('#something').attr('class','myValue');
Or even better use .prop()
instead of .attr()
$('#something').prop('class',myValue);
Or if you want to add class to an element then you can use .addClass()
$('#something').addClass(myValue);
Upvotes: 1