Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

alter external style sheet with jquery or javascript

Options
  • 08-12-2011 1:45pm
    #1
    Registered Users Posts: 46


    Hi I would like to change the background-color rule of an external stylesheet,just wondering how I would go about this,probably using .css method of jquery but anyway will do. any help

    Thanks.....


Comments

  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Do you want to change the actual physical file itself or just the style that is applied to a particular page element?


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    You can use either
    object.style.backgroundColor = '#000';

    or

    document.styleSheets[0].addRule('.selector', 'background-color:red;');


  • Registered Users Posts: 46 rambo85


    This is some of the external sheet,I have a function that I want to call to change the background color of body

    function changeStyle()
    {

    document.styleSheets[0].addRule('body','background-color:red;');
    }
    //end changeStyle function
    /*
    Design by Metamorphosis Design
    http://www.metamorphozis.com
    Released for free under a Creative Commons Attribution 2.5 License
    */
    
    *{
        margin: 0px;
        padding: 0px;
    }
    
    img, fieldset{
        padding: 0px;
        border: none;
    	margin: 0px;
    	line-height: 0px;
    }
    #grad_bg{
    	position: absolute;
    	height: 664px;
    	background-color: #ffffff;
    	width: 100%;
    }
    a{
        color: #ffffff;
        text-decoration: none;
    }
    
    a:hover {
        text-decoration: none;
        color: #000000;
    }
    
    body{
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 12px;
    	/* [disabled]line-height: 17px; */
    	color: #ffffff;
    	background-color:red;
    }
    


  • Registered Users Posts: 10,624 ✭✭✭✭28064212


    What's your reason for not just using this?:
    function changeStyle()
    {
      document.body.style.backgroundColor = 'red';
    }
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 339 ✭✭duffman85


    Keep it simple! -just use CSS - the last defined style is the one the page will use. so add style information after the link tag with a <style> tag or another link tag or add style directly to the element(<div style=".....">)

    add

    [PHP]
    <style type="text/css">
    body{background-color:red;}
    </style>
    [/PHP]
    after the<link> element for your stylesheet on your page.

    alternatively you can link another stylesheet

    [PHP]
    <link rel="stylesheet" href="defaultStyles.css" />
    <link rel="stylesheet" href="override.css" />
    [/PHP]
    If you define the some of same styles in the second linked stylesheet it will override the first one. i.e. the style for body is defined in the external stylesheet, putting body{background-color:red;} in the override.css will override this.


  • Advertisement
  • Registered Users Posts: 1,216 ✭✭✭carveone


    duffman85 wrote: »
    Keep it simple! -just use CSS - the last defined style is the one the page will use.

    That's the cascading bit. ;)


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    What duffman85 said. If it doesn't appear to work, look into the specificity of the styles applied.


  • Registered Users Posts: 46 rambo85


    I have to change it with javascript or jquery,its for a class asignment,
    its an external style sheet and i want to change the background colour,
    I tried using
    document.style.background-color='red'
    

    but it doesnt work,my lecturer said I have to change the external style sheet,I looked online for ways to do it,but had no luck


  • Registered Users Posts: 10,624 ✭✭✭✭28064212


    rambo85 wrote: »
    I have to change it with javascript or jquery,its for a class asignment,
    its an external style sheet and i want to change the background colour,
    I tried using
    document.style.background-color='red'
    

    but it doesnt work,my lecturer said I have to change the external style sheet,I looked online for ways to do it,but had no luck
    Changing an external stylesheet with Javascript doesn't make a whole lot of sense. Can you post the exact text of the question?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    rambo85 wrote: »
    I have to change it with javascript or jquery,its for a class asignment,
    its an external style sheet and i want to change the background colour,
    I tried using
    document.style.background-color='red'
    

    but it doesnt work,my lecturer said I have to change the external style sheet,I looked online for ways to do it,but had no luck

    I don't believe you can have properties such as background-color due to the dash (incorrect syntax). Only way you could do something like that would be through style["background-color"], but I don't believe that is the correct anyway.

    I believe you should go for document.body.style.background or document.body.style.backgroundColor.


  • Advertisement
  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    backgroundColor, the actual property, not the attribute that reflects the property.
    It would be style["backgroundColor"] either.


  • Registered Users Posts: 10,624 ✭✭✭✭28064212


    I don't believe you can have properties such as background-color due to the dash (incorrect syntax). Only way you could do something like that would be through style["background-color"], but I don't believe that is the correct anyway.

    I believe you should go for document.body.style.background or document.body.style.backgroundColor.
    D'oh! Well spotted. The correct one is document.body.style.backgroundColor

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Giblet wrote: »
    backgroundColor, the actual property, not the attribute that reflects the property.
    It would be style["backgroundColor"] either.

    Don't get your first line. Either the style["xxx"] or style.xxx would be valid.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Sorry, just highlighting the different between attributes and properties.


Advertisement