Using jQuery to Dynamically Add CSS to HTML

jQuery easily allows for CSS to be dynamically changed as a result of different event triggers. This is especially useful if you want to change some styling of a particular element after a click. Implementing this type of code is fairly simple. The code in the following example would change the color of the text within a div after a div is clicked:

JAVASCRIPT:
  1. <br />
  2. $('div').click(function(){<br />
  3. $('div p').css("color", "#000");<br />
  4. });<br />

In the above example, the .css() method takes two values -- the first should be the name of the CSS property you want to change, the second should be the property's new value. Make sure the values are separated by a comma (not a colon like they would be in your stylesheets), and make sure they're within quotes.

Continue Reading Below

Comments are closed.

Sorry, but comments for this entry are now closed.