Jump to content

Recommended Posts

I've tried searching and I'm not sure if this is even possible due to the lack of (good) results.

 

I'm trying to make it so that when you click a button the actual stylesheet changes. I want for example:

 

<style>h1{ color: #aa000; }</style>
<h1>Test</h1>

 

would obviously make h1 have a font color of aa0000. Well I'm trying to make a button that changes that one to 00aa00 instead which is not a problem. Very easy actually especially since I use jquery. However I also create new h1 elements through ajax and those ones use the original aa0000 color because of the stylesheet. I want to know how to make new elements have the new color also.

Link to comment
https://forums.phpfreaks.com/topic/237877-javascript-and-css/
Share on other sites

No, you can't modify the contents of the stylesheet. JavaScript can only modify the DOM - Document Object Model. Every element that makes up a web page, has a corresponding object within a parent 'document' object. So when you modify the color of an element, you only change that specific object within the DOM. What you should do is cater for such changes within your stylesheets, by creating separate classes for the elements. So when you click the button, you modify the element's class name, not it's individual properties. Any further elements you create you should associate with the new classes.

Link to comment
https://forums.phpfreaks.com/topic/237877-javascript-and-css/#findComment-1224587
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.