#7312 closed bug (wontfix)
add class does not work for multiple calls
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | low | Milestone: | 1.5 |
Component: | attributes | Version: | 1.4.3 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Hi jQuery team. First of all, i very much like the job that you are doing and please do keep on doing this. I have one question about addClass method when multiple called on the same element. If I use pure JS like this
<style type="text/css">
#test{width:400px;}
.blue{background-color:Blue;}
.red{background-color:Red;}
</style>
... document.getElementById('test').className = 'blue';
document.getElementById('test').className = 'red'; document.getElementById('test').className = 'blue'; document.getElementById('test').className = 'red'; document.getElementById('test').className = 'blue'; document.getElementById('test').className = 'blue'; document.getElementById('test').className = 'blue';
it works just fine. But when i use jQuery for the same call...
$('#test').addClass('blue'); $('#test').addClass('red'); $('#test').addClass('blue');
it does not affect the element with the new class. Can you please explain to me why is acting like this? Is this as it proper way of working or is it something else? Thank you in advance.
Change History (5)
comment:1 Changed 12 years ago by
Component: | unfiled → attributes |
---|---|
Owner: | set to [email protected]… |
Status: | new → pending |
comment:3 Changed 12 years ago by
Priority: | undecided → high |
---|---|
Status: | pending → open |
comment:4 Changed 12 years ago by
Using .className
sets the class attribute to the specified value. addClass only adds the class if it's not present. You should use a removeClass('red') before adding blue
to the element.
comment:5 Changed 12 years ago by
Priority: | high → low |
---|---|
Resolution: | → wontfix |
Status: | open → closed |
Can you distill this into a reduced test case on jsFiddle - thanks!