Editing CSS using JQuery

Published: 5/06/2009
 
JQuery can be used to edit or CSS quick and easily. It's a truly amazing and simple tool that gives you peice of mind when dealing with cross compatibility.   Assigning CSS Rule to an HTML Elements
If you have the CSS rule already defined then it's very easy to assign the rule to any elements within the document.

<style type="text/css">
a.test { font-weight: bold; }
</style>
<div id="a">This is a test</div><br>
$("a").addClass("test");

Above is a simple Style Sheet rule called "test". We can assign this rule to an elements that's available to JQuery. The second part of the example is assigning the rule "test" to the element "a".
 
I don't have the Rules
Sometimes it's not possible (or relevent) to write a CSS rule for the element you are trying to change. In this case you can always place the rule directly into the elements.
 
If you have the CSS rule already defined then it's very easy to assign the rule to any elements within the document.

<style type="text/css">
a.test {
font-weight: bold;
}
</style>
<div id="a">This is a test</div>

$("#a").addClass("test");


Above is a simple Style Sheet rule called "test". We can assign this rule to an elements that's available to JQuery. The second part of the example is assigning the rule "test" to the element "a".
 
$('#a').css("font-weight","bold");
 
This does the same thing but without the requirements of a pre-made CSS rule!
 

Related Posts

JQuery Toggle

Last Updated: 6/05/2010
Sometime a simple toogle needs a little more work. I was looking for a solution that allowed the toggle to fade a element and to slide a element. I found a great little Plugin from Gregoro Magini but it needed a little work to do what I wanted to do.
Read more...

 

Using AJAX on Forms

Last Updated: 29/04/2010
JQuery is a very flexible tool for managing form data. There is a plugin that allows JQuery to manage form data passing this data using AJAX.
Read more...

 

Zoom In Animation

Last Updated: 18/03/2009
Using JQuery to animate a given ID element.
Read more...