RSS Forums RSS
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 843 | Replies: 2
Reply
Join Date: Apr 2007
Location: Portugal
Posts: 26
Reputation: DeOiD is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DeOiD DeOiD is offline Offline
Light Poster

Help javascript and css

  #1  
Jul 24th, 2007
hi again

is there a way to change css rules using javascript? i have this:

<style type="text/css">
.flash
{
position:absolute;
left:52px;
bottom:30px;
}
.logo
{
position:absolute;
left:0px;
top:20px
}
</style>


and i want to change .logo and .flash attributes depending on the window size...

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,054
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 372
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: javascript and css

  #2  
Jul 24th, 2007
No, as far as I know, you can't do that. But you can surely get hold of all the elements which use that class and change their property which is what you must be aiming for, methinks.

<html>
<head>
    <style>
        .one
        {
            background-color: blue;
        }
        .two
        {
            background-color: green;
        }
    </style>
    <script>
    getElementsByClassName = function (needle)
    {
      var         my_array = document.getElementsByTagName("*");
      var         retvalue = new Array();
      var        i;
      var        j;

      for (i = 0, j = 0; i < my_array.length; i++)
      {
        var c = " " + my_array[i].className + " ";
        if (c.indexOf(" " + needle + " ") != -1)
          retvalue[j++] = my_array[i];
      }
      return retvalue;
    }

    function changeColor(name, color)
    {
        var elements = getElementsByClassName(name);
        for(var i = 0; i < elements.length; ++i)
        {    
            if(elements[i])
                elements[i].style.backgroundColor = color;
        }
    }
    </script>
</head>
<body>
    <form>
    <p class="one">Hello to all</p>
    <p class="two">Someone is here</p>
    <p class="one">Really? Who is he?</p>
    <p class="two">Do I know him?</p>
    <input type="button" value="Blue to Red" onclick="changeColor('one', 'red');" />
    <input type="button" value="Green to Yellow" onclick="changeColor('two', 'yellow');" />
    </form>
</body>
</html>
Last edited by ~s.o.s~ : Jul 24th, 2007 at 1:25 pm.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jul 2007
Posts: 8
Reputation: Voynex is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Voynex Voynex is offline Offline
Newbie Poster

Re: javascript and css

  #3  
Jul 24th, 2007
Yes, it is not possible to modify rules in the CSS file, but you can modify the style of the DOM node.
voynex.com - high quality software and web development.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:19 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC