Click here to Skip to main content
11,704,768 members (50,126 online)
Click here to Skip to main content

Tagged as

Handling CSS by using jQuery

, 21 Dec 2011 CPOL 4K 1
Rate this:
Please Sign up or sign in to vote.


Handle CSS using jQuery/Javascript
Here are two alternatives. In jQuery, you may use attr method to set attribute values of selected elements. In JavaScript, you may use className property to do the same thing.


<title>Handle CSS using jQuery/Javascript</title>
<script src="jquery-1.7.1.min.js"></script>
<style type="text/css">
table {background-color:gray;}
div {font-weight:bold;color:#ffffff;height:50px;width:170px;border:solid 2px #ffffff;}
.classBLUE {background-color:#0000ff;color:#ffffff;}
.classRED {background-color:#ff0000;color:#ffffff;}
.classGREEN {background-color:#00cc00;color:#ffffff;}
<script type="text/javascript">
function setGreenjQuery()
    $("#divRGB").attr("class", "classGREEN");
function setBluejQuery()
    $("#divRGB").attr("class", "classBLUE");
function setRedjQuery()
    $("#divRGB").attr("class", "classRED");
function setGreenJs()
    document.getElementById("divRGB").className = 'classGREEN';
function setBlueJs()
    document.getElementById("divRGB").className = 'classBLUE';
function setRedJs()
    document.getElementById("divRGB").className = 'classRED';
<table width="170px">
<td colspan="3" align="center">
<div id="divRGB">Handle CSS</div>
<td colspan="3" align="center">By jQuery</td>
<td><input type="button" value="Red" onclick="setRedjQuery()"/></td>
<td><input type="button" value="Green" onclick="setGreenjQuery()"/></td>
<td><input type="button" value="Blue" onclick="setBluejQuery()"/></td>
<td colspan="3" align="center">By Javascript</td>
<td><input type="button" value="Red" onclick="setRedJs()"/></td>
<td><input type="button" value="Green" onclick="setGreenJs()"/></td>
<td><input type="button" value="Blue" onclick="setBlueJs()"/></td>

Browser Compatibility

I have tested this code in the following Web browsers:
  • Internet Explorer
  • Mozilla Firefox
  • Google Chrome
  • Safari
  • Opera

Further Reading


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
Programmer | Dude | ||||||

About him & his blog? check out here. Currently he's updating the site, he'll return soon there with more things*.

In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon.

Favorite techs are MVC, WCF, Mobile, HTML5, CSS3, jQuery, etc.,

His hobbies are watching movies, CP, being alone, etc., But now he's working on something*.

* - SCAMP???

You may also be interested in...

Comments and Discussions

GeneralThis will break the css if the element has more classes assi... Pin
Daniel Gidman21-Dec-11 7:27
memberDaniel Gidman21-Dec-11 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 21 Dec 2011
Article Copyright 2011 by thatraja
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid