stylesheets minipulation - Question: How to change a string to a property name.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

    stylesheets minipulation - Question: How to change a string to a property name.

    OK.
    I have made a function here that will suposedly go down the stylesheet and change particular properties. However I want to be able to make the property name defined when the function is called.

    Function:
    [CODE=javascript]
    function changeRule(theN umber, property, value) {
    var theRules = new Array();
    if (document.style Sheets[0].cssRules) {
    theRules = document.styleS heets[0].cssRules;
    } else if (document.style Sheets[0].rules) {
    theRules = document.styleS heets[0].rules;
    }

    theRules[theNumber].style.[property] = value;
    }
    [/CODE]
    Basic Function call:
    [CODE=javascript]
    changeRule(1, 'backgroundImag e', 'url(./images/corner2.gif)');
    [/CODE]
    so basicly I want the "property" variable to apear as the propertyname. How can i do this? any other suggestions to improve this code?
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by Jezternz
    [CODE=javascript]
    var theRules = new Array();
    [/code]
    It's already an array so don't need that.
    Code:
        if (document.styleSheets[0].cssRules)
    Don't blindly assume that document.styleS heets is supported
    Code:
        theRules[theNumber].style.[property] = value;
    theRules[theNumber].style[property] = value;


    [CODE=javascript]function changeRule(theN umber, property, value)
    {
    var theRules, ss=document.sty leSheets||null;

    if(ss && ss.length)
    {
    ss=ss[0];
    if( theNumber < (theRules=ss.ru les||ss.cssRule s).length)
    theRules[theNumber].style[property] = value;
    }
    }[/CODE]

    Comment

    • Jezternz
      New Member
      • Jan 2008
      • 145

      #3
      ok thanks heaps man. Much apreciated. Can I ask if there is an alternative to stylsheets? This method seems very bitsy, I would like a cleaner script or is this not possible with todays browsers.

      By the way the whole reason I am using style sheets is because I want to change the properties of elements like follows .class #id ellement{} eg .big #bigger h1{}
      and u cant do this using the standard getelementbyid.
      So if theres anotehr way, please tell me :).

      Thanks again, Josh

      Comment

      Working...