7

I want to change the dimensions of a set of elements that are not created yet. They will be created by a JavaScript script that I don't have access to. JQuery's css() function would apply the changes only on existing items, while I want the code to work like if I had set CSS properties in a CSS file.

Can anyone help me do it?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
André
  • 287
  • 2
  • 16

1 Answers1

5

One option would be to dynamically create a style element:

$("<style type='text/css'> .redbold{ color:#f00; font-weight:bold;} </style>").appendTo("head")
$("<div/>").addClass("redbold").appendTo("body");

Taken from here: Create a CSS rule / class with jQuery at runtime.

Here's a possible IE alternative (see Creating a CSS class in jQuery):

document.styleSheets[0].addRule('body', 'background: green', -1);

For reference, see the documentation for addRule.

Community
  • 1
  • 1
dyersituations
  • 128
  • 1
  • 7
  • can't insert css rules directly into head tag without a style tag. Older IE versions don't do well with appended style tags either – charlietfl Nov 10 '12 at 21:27
  • @charlietfl - I messed up when pasting in the code (new to SO). Also added a possible alternative for IE. – dyersituations Nov 10 '12 at 21:34