Altering CSS Class Attributes with JavaScript
This JavaScript function will dynamically change the style information for any CSS class in a Web Page. Applying this function to a class will cause all items calling on the class to change to the updated style value. This function works even if there are multiple stylesheets referenced in the document.
Function Name: changecss(theClass,element,value)
Example of output (Assuming you have a stylesheet defining the "exampleA" and "exampleB" classes -- the following code creates the effect below):
<span class="exampleA">Example A</span> <span class="exampleB">Example B</span> <span class="exampleA">Example A</span> <input type="button" value="Change A Red" onclick="changecss('.exampleA','color','red')"/><input type="button" value="Change A Black" onclick="changecss('.exampleA','color','black')"/>
Example A
Example B
Example A
To use this function, copy this link and place it in the head of your HTML before any other JavaScript that may use it.
Please include credit when using this script and read this site's Terms & Conditions before using.
This function was last updated on October 9, 2010. It has been tested and works in the following browsers:
- Internet Explorer 6+ (Windows)
- Firefox 3+ (Windows & Linux)
- Google Chrome 2.0.172.33 + (Windows)
- Safari 3.1.1 + (Windows)
- Opera 9.64 +(Windows)
Note that I have tested these browsers on Windows; this function works in Firefox in Linux as well. I assume that the latest Mac versions work the same.
- Free Useful Javascripts
Links to articles that give free javascript code or javascript examples.
- Altering CSS Class Attributes with JavaScript
- Change HTML Styles with JavaScript
- Select Some Checkboxes JavaScript Function
- Select All Checkboxes in a Form with JavaScript
- Get a Form Element's Label with JavaScript
- Select Radio Inputs JavaScript
- UseMaps Crash IE When Changed
- Hide Select Menus JavaScript
- Manipulating Element Styles Based on CSS Classes Using Prototype
- Compare Values with Arbitrary Comparison Operator
- Related Topics
Vikas
Mar 18, 2014
Reply
Shawn Olson
Mar 22, 2014
Reply
Thomas Ng
Mar 11, 2012
Reply
Shawn Olson
Mar 19, 2012
Reply
Andrew Cox
Dec 20, 2012
Reply
Shawn Olson
Dec 20, 2012
Reply
Jason
Mar 9, 2012
Reply
Shawn Olson
Mar 10, 2012
Reply
Jason
Mar 12, 2012
Reply
Shawn Olson
Mar 19, 2012
Reply
Eric
Jan 4, 2012
Reply
vishal
Dec 29, 2011
Reply
Shawn Olson
Jan 4, 2012
Reply
mario santini
May 17, 2011
Reply
Shawn Olson
Nov 3, 2011
Reply
James Matthews
Nov 9, 2010
Reply
YesHoney
Sep 28, 2010
Reply
Anfernee Lo
Sep 23, 2010
Reply
Luis Stortini
Jul 9, 2010
Reply
Parul Desai
May 12, 2010
Reply
Parul Desai
May 12, 2010
Reply
Jon Harris
Apr 7, 2010
Reply
Khaled Al Hourani
Mar 5, 2010
Reply
Hassaan Abd-ur-Razzaq
Dec 7, 2009
Reply
Shawn Olson
Dec 7, 2009
Reply
Will Bradley
Aug 20, 2009
Reply
Aleksnandr Tishin
Aug 15, 2009
Reply
manish kumar
Jul 23, 2009
Reply
Lennart Moltrecht
May 31, 2009
Reply
Shawn Olson
Jun 23, 2009
Reply
jacob
Aug 26, 2010
Reply
hariharadeep m
Jul 28, 2008
Reply
Shawn Olson
May 20, 2008
Reply
Reinier Post
Jul 4, 2008
Reply
Kurt Munro
Jan 10, 2009
Reply
Shawn Olson
Jan 10, 2009
Reply
Brian H
May 20, 2008
Reply
Gliktch Media
Nov 6, 2009
Reply
Gary E
Nov 30, 2007
Reply
Simon Wilsher
Oct 26, 2007
Reply
Gary E
Nov 30, 2007
Reply
Dag Fredrik
May 30, 2008
Reply
chris tillam
Jul 2, 2007
Reply
chris tillam
Jul 2, 2007
Reply
Lya santoso
Feb 18, 2007
Reply
Parul Desai
May 12, 2010
Reply
Brian French
Feb 6, 2007
Reply
Shawn Olson
Dec 21, 2006
Reply
vee Jay
Feb 15, 2011
Reply
Justin Halsall
Dec 21, 2006
Reply
Udo Willers
Dec 30, 2006
Reply
Alex Egg
Feb 2, 2007
Reply