jQuery comes with addClass() and removeClass() to add or remove CSS class dynamically. For example,

  1. $(‘#para1′).addClass(‘highlight’); – Add a “highlight’ css class to elements that contain id of “para1″.
  2. $(‘#para1′).removeClass(‘highlight’); – Remove a “highlight’ css class from elements that contain id of “para1″.


<style type="text/css">
  .highlight { 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  <h1>jQuery add / remove css class example</h1>
  <p id="para1">This is paragraph 1</p>
  <p>This is paragraph 2</p>
  <p>This is paragraph 3</p>
  <p>This is paragraph 4</p>
  <button id="addClass">Add highlight</button>
  <button id="removeClass">Remove highlight</button>
<script type="text/javascript">
    $("#addClass").click(function () {
    $("#removeClass").click(function () {
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts