SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamically defining CSS values

    Alright, here's another one for you guys.

    I have a style property, like "backgroundColor", or "width" stashed in a variable called styleName. Now, I need to dynamically set that property's value for a particular page element, like so:

    document.getElementById("whateverElementId").style.styleName ="whatever";

    The drawback is, well, it doesn't work. ;-)

    Any suggestions?

  2. #2
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah!

    document.getElementById("whateverElementId" ).style[styleName] ="whatever";

  3. #3
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use .className to specify a new style, too:
    HTML Code:
    <style>
    	style_one {
    		color: #FF8080;
    	}
    
    	style_two {
    		color: #8080FF;
    	}
    </style>
    
    <script>
    	document.getElementById ( 'someid' ).style.className = 'style_two';
    </script>
    Of course, that's just my opinion. I could be wrong.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •