views:

69

answers:

3

Is it possible to define a class with specific style attributes dynamically with jQuery, rather than setting the style of all elements with that class?

I could set the attributes of the class at the end of the script once all the elements have been created, but is that the best way to go about it? If I define the style of the class with $('.class').css('property','value'); at the beginning of the script, nothing would happen because the elements with class .class haven't been created yet, right?

A: 

Not sure what you're reasoning for this is, but yes - you have to call the script once the elements have been created. If you don't want to have the script at the end of your document, wrap it in

$(document).ready(function() {
 ...
});

or, alternatively, the shorthand

$(function() {
 ...
});

So it fires once all the HTML is loaded.

Jeriko
I'm generating the elements on my page with jQuery, and I want to make my code tidier by using classes instead of specifying all the style properties with `css()`. The thing is I want to set the properties of those classes dynamically. Also, the elements in question are being generated in `$(document).ready(function(){});`
Acorn
Man, getting a -1 for attempting to answer a vague question really stings. Are you looking for `.addClass()`? No more answers from this side, for fear of more punishment :P
Jeriko
Sorry if my question was vague, tried my best to explain, you didn't deserve -1. Pointy's answer and patrick's edit hit the nail on the head.
Acorn
+3  A: 

If I'm understanding you, you want to add style to specific elements.

You don't have to use .css(a,b) to do this. jQuery has a function called addClass("className"); that will add a class to whatever element you want.

$('.originalClass').addClass('addedClass');

It doesn't overwrite the original.

Is that what you wanted?


EDIT:

Or are you saying that you want to modify your stylesheet in javascript?

If that's the case, there are javascript rules for modify stylesheet properties.

If you (for some reason) can't change the stylesheet, then maybe you could simply store your styles in an object, and apply that.

var stylesToAdd = { background:"orange", border:"1px solid blue" }

And use .css() to apply them.

$('#myElement').css(stylesToAdd);
patrick dw
He wants to be able to make up style information for elements with a certain class, and then have those styles take effect dynamically. And he doesn't want to do it by having jQuery amend each element's style directly.
Pointy
The problem was specifying the style of that class in my javascript.
Acorn
I see. Pointy's solution would work, or I added another option.
patrick dw
+5  A: 

The proper way to do that is to use CSS. If you want elements with class "foo" to have a bunch of properties, then put those properties in a CSS file and associate them with an appropriate selector:

.yourClass {
  font-weight: bold;
  color: green;
}

If you need to do this dynamically, it's possible to write CSS blocks with Javascript. Put a <style> element in your page and give it an "id" value:

<style id='dynamicStyle'></style>

Then you can set it with jQuery:

$('#dynamicStyle').text(".yourClass { font-weight: bold; }");
Pointy