JQuery how-to's

Changing CSS styles

Changing the style of an element is very useful to show a reaction to a user event, or just to set up the look and feel of an element. jQuery provides the .css() method for this. The simplest form of this method is to specify a style as a parameter. With this single parameter jQuery will tell use the current value for that style. If we add a second parameter, jQuery will set the style in the first parameter to the value of the second parameter (key/value).

alert( $("#mydiv").css("font-family") );
$("#mydiv").css("font-family", "sans-serif");

This is fine when we only want to set a single value. When we want to set multiple styles we have the option of passing an object as the only parameter, with each property of the object being the style, and the property value being the value to set.

$("#mydiv").css({
"font-family" : "sans-serif",
"font-weight" : "bold",
"border" : "1px solid red"
});
 
/home/dodgydevil/wiki.zendfusion.com/data/pages/scripts/js/jquery.txt · Last modified: 2009/08/19 01:36 by bosbaba
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
Zend Fusion