JavaScript DOM, need help to manually create a CSS style in <head>

Let's say I wanted to create a CSS style within a page that looks like this:

.someStyle {
    background: #FF0000;

I'm trying to use JavaScript DOM to create it, and I have something like:

var someStyle= document.createElement('someStyle');
someStyle.type = 'text/css';
someStyle.innerHTML = '.cssClass { color: #FF0000; }';

Open in new window

But it's not working because I'm not seeing anything appear in the page through the webkit browser debugger at all. I need this created so that I can use jQuery's addClass("someStyle") inside my code. I would appreciate any help. Thanks.

P.S. Yes, I need to do it this way, please don't suggest using the .css() method of jQuery, it does not fulfill what I'm trying to do.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
This code worked in my test, Chrome, Firefox and Safari (don't have IE):
var head = document.head || document.getElementsByTagName('head')[0],
	    style = document.createElement('style'),
	    css = '.cssClass { color: #FF0000; }';
	    style.type = 'text/css';
	if (style.styleSheet){
	  style.styleSheet.cssText = css;
	} else {


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Works like a charm, Tom! Thanks!

Strange though, when I look at the code through Chrome's webkit, I do not see the newly-created style appearing within the <style> tag at all. To your knowledge, is that the way it really is?
Tom BeckCommented:
Not on my system. All three browser developer tools show the dynamically created style bock in the DOM. This is chrome. Be sure you are looking at the DOM, not the "view source".
Chrome developer toolsThanks for the points.
Tom BeckCommented:
If you prefer, you can add the new definition to an existing <style> block if one exists by changing the following line.
style = document.getElementsByTagName('style')[0] || document.createElement('style'),

Open in new window

It will be added to the first <style> block in the head section.
elepilAuthor Commented:
Ah, I see it now. I was at the Sources tab, I had to switch over to the Elements tab to see the DOM. Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.