JavaScript
  Home arrow JavaScript arrow Page 2 - Adding and Removing CSS Classes with the j...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Adding and Removing CSS Classes with the jQuery JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-09-16

    Table of Contents:
  • Adding and Removing CSS Classes with the jQuery JavaScript Library
  • Adding and removing CSS classes on the fly
  • Dynamically adding a CSS class to a predefined web page element
  • Creating some basic mouse effects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Adding and Removing CSS Classes with the jQuery JavaScript Library - Adding and removing CSS classes on the fly


    (Page 2 of 4 )

    As I mentioned in the beginning, “jQuery” comes equipped with a couple of methods for dynamically manipulating the CSS classes of a targeted element of a web page. The first one of these is simply called “addClass().” It attaches a CSS class to a selected element.

    The prototype for using this method can be seen in the following code sample, so have a look at it, please:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Basic example on using jQuery with addClass() method</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: 24px bold Arial, Helvetica, sans-serif;

    color:#000;

    }

    .largetext{

    font: normal 22px Arial, Helvetica, sans-serif

    }

    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    $("a").addClass('largetext')

    });

    </script>

    </head>

    <body>

    <h1>Basic example on using jQuery with addClass() method</h1>

    <a href="http://www.devshed.com/">Visit Devshed.com now!</a>

    </body>

    </html>

    As shown above, the “addClass()” method can be really helpful for assigning a specific CSS class to a given web page element. In the previous example, a “largetext” class is dynamically tied to a link, after the whole web document has been loaded. Pretty simple to grasp, isn’t it?

    On the other hand, jQuery provides another method, called “removeClass(),” that performs exactly the opposite operation -- it unties a CSS class from a selected web page element. The way that this method works will be better understood if you examine the following code sample. Here it is:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Basic example on using jQuery with removeClass() method</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: 24px bold Arial, Helvetica, sans-serif;

    color:#000;

    }

    .smalltext{

    font: normal 11px Arial, Helvetica, sans-serif

    }

    </style>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    $("a").removeClass('smalltext')

    });

    </script>

    </head>

    <body>

    <h1>Basic example on using jQuery with removeClass() method</h1>

    <a href="http://www.devshed.com/" class="smalltext">Visit Devshed.com now!</a>

    </body>

    </html>

    That wasn’t rocket science! As you can see, the “removeClass()” method has been used to remove a “smalltext” CSS class from a sample link, after the entire web document has finished loading.

    So far, so good. At this point, assuming that you've already grasped how the  “addClass()” and “removeClass()” methods do their business, it’s time to more deeply explore the actual functionality of these methods, since they allow us to work with more complex CSS selectors in a simple fashion.

    Therefore, in the following section, I’m going to show you how to use this useful characteristic to dynamically add a CSS class to a selected element within a web page.

    To learn the complete details for how this process will be achieved, please click on the link that appears below and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This sixth part of the series discusses how to assign and remove CSS classes...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek