Bug Tracker

Ticket #7312 (closed bug: wontfix)

Opened 4 years ago

Last modified 3 years ago

add class does not work for multiple calls

Reported by: igor.lazarevski@… Owned by: igor.lazarevski@…
Priority: low Milestone: 1.5
Component: attributes Version: 1.4.3
Keywords: Cc:
Blocking: Blocked by:

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

comment:1 Changed 4 years ago by rwaldron

  • Owner set to igor.lazarevski@…
  • Status changed from new to pending
  • Component changed from unfiled to attributes

Can you distill this into a reduced test case on jsFiddle - thanks!

comment:3 Changed 4 years ago by rwaldron

  • Priority changed from undecided to high
  • Status changed from pending to open

comment:4 Changed 4 years ago by Utkarsh

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 4 years ago by rwaldron

  • Priority changed from high to low
  • Status changed from open to closed
  • Resolution set to wontfix
Note: See TracTickets for help on using tickets.