<div class="bestAnswerControl"> <div id="ct100_contentplaceholder_lvanswer_control_divbestanswer" class="IsBestAnswer"></div> </div>
I want to add:
.bestanswer { // some attribute }
I want to replace class="IsBestAnswer"
of div to class="bestanswer"
by jquery. How do I do this?
I am using this approach:
$('.IsBestAnswer').addclass('bestanswer');
but it's not working.
To change all classes for an element:document. getElementById("MyElement"). className = "MyClass"; (You can use a space-delimited list to apply multiple classes.)
To replace a class with another class, you can remove the old class using jQuery's . removeClass() method and then add the new class using jQuery's . addClass() method.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
$('.IsBestAnswer').addClass('bestanswer').removeClass('IsBestAnswer');
Case in method names is important, so no addclass
.
jQuery addClass()
jQuery removeClass()
Instead of removeClass and addClass, you can also do it like this:
$('.IsBestAnswer').toggleClass('IsBestAnswer bestanswer');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With