0

How to remove current class added to div and add new class according to condition set?

My css contain 3 different classes:

.red   { background:red }

.blue  { background:blue }

.green { background:green } 

at the time I want to add only one class to div id #box according to condition set by jquery variable my_color .my_color has 3 values blood,garden,sky only one set at the time.

jquery:

<script type="text/javascript">  
 (document).ready(function() {

      if(my_color == "blood")
             { /*help me to remove  current class and add .red */}
      else if(my_color == "garden")
             {/*help me to remove  current class and add .green */ }
      else if(my_color == "sky")
             {/*help me to remove  current class and add .blue */ }

 });
</script>
1

6 Answers 6

1

this replaces the class on box with red/green/blue

   if(my_color == "blood")
           {  $('#box').attr('class','red');}
      else if(my_color == "garden")
            { $('#box').attr('class','green');}
      else if(my_color == "sky")
         {   $('#box').attr('class','blue');}
Sign up to request clarification or add additional context in comments.

Comments

1
$('#box').attr('class', '').addClass(my_color );

Comments

1

You can remove all classes before assigning a new one:

 (document).ready(function () {
     $(element).removeClass("red blue green");
     if (my_color == "blood") {
         $(element).addClass("red");
     }
     if (my_color == "garden") {
         $(element).addClass("green");
     }
     if (my_color == "sky") {
         $(element).addClass("blue");
     }
 });

Using .attr('class', '') instead of removeClass will work too, but it will also remove any other classes your element might have.

You can also use a mapping object to clean up the code a bit:

 (document).ready(function () {
     $(element).removeClass("red blue green");
     var colorDictionary = {
         "blood": "red",
         "garden": "green",
         "sky": "blue"
     };
     $(element).addClass(colorDictionary[my_color]);
 });

3 Comments

Thanks matt, just trying it
using .attr('class', '') removes all other classes added to div . so better is to remove only particular class.
@galexy Yes. Suppose you wanted to add another class that would specify the size and of your element. Every time you call set the class attribute to "" all the classes will be removed and the element will revert back to its default styles. If you just remove the classes used for the setting the color the class specifying the size of the element will remain untouched.
0

you can do this with $.removeAttr() and $.addClass().

At first remove all the classes set to the #div and then only apply the needed class. To be more specific you can only remove the unwanted class with $.removeClass()

 (document).ready(function() {

   $("#box").removeAttr("class");

      if(my_color == "blood")
         { 
            $("#box").addClass("red"); 
         }
      else if(my_color == "garden")
         { 
            $("#box").addClass("green"); 
         }
      else if(my_color == "sky")
         { 
            $("#box").addClass("blue"); 
         }

 });

Comments

0
<script type="text/javascript">  
  (document).ready(function() {

  if(my_color == "blood")
         {$("#select_element").removeClass("blue").addClass("red");}
  else if(my_color == "garden")
         { $("#select_element").removeClass("red").addClass("green"); }
  else if(my_color == "sky")
         { $("#select_element").removeClass("green").addClass("blue"); }

  });
 </script>

Comments

0
$('#selectorId').removeClass('red');
$('#selectorId').addClass('green');

Comments

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.