Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add and remove css class

Tags:

jquery

how to remove CSS default class

This is my div

<div id="messageContainer">

This is my css class

#messageContainer{  
  height:26px;  
  color:#FFFFFF;    
  BACKGROUND-COLOR: #6af; 
  VERTICAL-ALIGN: middle;
  TEXT-ALIGN: center;
  PADDING-TOP:6px;  
}

I want to remove default class and new css class

Please help;

like image 815
Vicky Avatar asked Oct 23 '25 18:10

Vicky


2 Answers

You aren't dealing with a class. You have default rules applied to the ID element. As such, you should really only need to add the new class:

$("#messageContainer").addClass("newRules");

Any rules that aren't overwritten can be overwritten with the css() method:

$("#messageContainer").css({
  'font-weight':'bold',
  'color':'#990000'
}).addClass("newRules");
like image 55
Sampson Avatar answered Oct 26 '25 09:10

Sampson


You can approach it two ways. One, use two classes and literally swap them out for each other:

.red { background: red }
.green { background: green }

And then in jQuery:

$("#messageContainer").attr('class','green'); // switch to green
$("#messageContainer").attr('class','red'); // switch to red

Or you can use CSS order to toggle a single class:

#messageContainer { background: red }
#messageContainer.green { background: green }

Then:

$("#messageContainer").toggleClass("green");

That would alternate backgrounds every time it was called.

like image 29
Doug Neiner Avatar answered Oct 26 '25 09:10

Doug Neiner