Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery CSS: Dynamically change attributes of a class

Tags:

jquery

css

I want to change the attribute of a class on which all element that use the class for the rest of the web application life (from start of use until the user exits the web application) will be affected.

html:

<p class="myClass">     What? </p> <p class="myClass">     Now? </p> 

css:

.myClass{     background-color: #ffff00;     } 

js:

$(".myClass").css("background-color", "#00FFFF"); $("p").last().after("<div class='myClass'>Now!</div>"); 

Here is a sample

What I want to achieve from the sample is that all subsequent dynamically added myClass will have the new attribute. From the jsFiddle, you'll see that the next added element doesn't have the new attribute.

MORE:

I'm just using the color for a basis, I will implement this in larger scale, what I want to accomplish is to dynamically change the attributes of a class that will be used for the entire web app life cycle.

like image 661
Marl Avatar asked Jan 23 '13 10:01

Marl


People also ask

How to set CSS dynamically in jQuery?

jQuery comes with addClass() and removeClass() to add or remove CSS class dynamically. For example, $('#para1'). addClass('highlight'); – Add a “highlight' css class to elements that contain id of “para1”.

How do you change a class value in CSS?

You can use className to assign a value directly to the class. Note: If any classes already exist on the element, this will override them. You can add multiple space delimited classes using the className property, or use it without assignment operators to get the current value of the class on the element.

Which function in jQuery can be used to change the style of an element dynamically?

The css() method is used to change style property of the selected element. Here we have created two elements inside body tag i.e. <h1> and <h3> elements. We apply CSS property on <body> tag and <h1> tag using css() method dynamically.

Can we change CSS property value using JavaScript or jQuery?

It is possible to change the CSS property of an element by using a simple JavaScript API, but we try to complete this challenge using jQuery css() method. Syntax: $(). css(propertyname, value); $().


2 Answers

I just stumbled upon this question, and thought I would chime in, even though it is almost a year old...

You actually can modify the css rules dynamically at runtime, just like any other DOM element. I don't think jQuery supports it, so you would need to use the native DOM elements directly.

You can assign titles to stylesheets to make them easier to locate in the DOM tree, but with a little forethought, it's usually not too difficult to find the rules you want. Here is a quick example from a jsfiddle based on your example.

function getStyleRule(name) {   for(var i=0; i<document.styleSheets.length; i++) {     var ix, sheet = document.styleSheets[i];     for (ix=0; ix<sheet.cssRules.length; ix++) {         if (sheet.cssRules[ix].selectorText === name)             return sheet.cssRules[ix].style;     }   } return null; }  var rule = getStyleRule('.myClass'); rule.backgroundColor = '#0ff';  $("p").last().after("<div class='myClass'>Now!</div>"); 

Here is a fiddle that shows your example, updated to modify the css rule itself: http://jsfiddle.net/93wGJ/5/

For more info on the DOM for stylesheets, have a look at http://www.w3.org/TR/DOM-Level-2-Style/stylesheets.html

like image 156
DaveInMaine Avatar answered Sep 21 '22 19:09

DaveInMaine


Using the css() method changes the inline styles on already existing elements, and you can't use that to change the styles on future elements. A workaround (that I don't like very much) would be to insert a style tag:

$( "<style>.myClass {background-color : #00FFFF}</style>" ).appendTo( "head" ) $("p").last().after("<div class='myClass'>Now!</div>"); 

FIDDLE

like image 44
adeneo Avatar answered Sep 19 '22 19:09

adeneo