473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript changing CSS color

48 New Member
I'm working with the following code, trying to change the color of the links inside the divs. While the background color and border change exactly as they're supposed to, the text/link color itself doesn't.

This is starting to drive me bonkers.

Ideas please?

Expand|Select|Wrap|Line Numbers
  1. <div class="style1">Gallery
  2.                                 <div class="style2">
  3.                                     <table cellpadding="5" width="50" style="font-size:11px;" class="style3">
  4.                                         <tr><td onMouseOver="alt(this)" onMouseOut="alt(this)"><a href="/flash/studio/">Studio</a></td></tr>
  5.                                         <tr><td onMouseOver="alt(this)" onMouseOut="alt(this)"><a href="/flash/outdoor/">Outdoor</a></td></tr>
  6.                                         <tr><td onMouseOver="alt(this)" onMouseOut="alt(this)"><a href="/flash/athletic/">Athletic</a></td></tr>
  7.                                         <tr><td onMouseOver="alt(this)" onMouseOut="alt(this)"><a href="/flash/event/">Event</a></td></tr>
  8.                                         <tr><td onMouseOver="alt(this)" onMouseOut="alt(this)"><a href="/flash/lifestyle/">LifeStyle</a></td></tr>
  9.                                     </table>
  10.                                 </div>
  11.                             </div>

Expand|Select|Wrap|Line Numbers
  1. .style1 {
  2.         position:relative; 
  3.         top:0px; 
  4.         left:0px;
  5.         padding: 0px;
  6. }
  7.  
  8. .style2 {
  9.         position:absolute; 
  10.         top:12px; 
  11.         left:0px; 
  12.         visibility:hidden;
  13.  
  14. .style3 td {
  15.         background-color:#6D6E71; 
  16.         color:white;
  17.         text-align: center;
  18.         border: 1px solid white;
  19.  
  20. .style3 a:link { color: white; text-decoration: none; font-weight: normal }
  21. .style3 a:active { color: white; text-decoration: none; font-weight: bold }
  22. .style3 a:visited { color: white; text-decoration: none; font-weight: normal }
  23. .style3 a:hover { color: white; text-decoration: none; font-weight: normal; }
  24. }
  25. }
Expand|Select|Wrap|Line Numbers
  1. function alt(obj)
  2. {
  3.     if (obj.style.backgroundColor=='white')
  4.     {
  5.         obj.style.backgroundColor='#6D6E71';
  6.         obj.style.color='#FF0000';
  7.         obj.style.border='1px solid white';
  8.     }
  9.  
  10.     else
  11.     {
  12.         obj.style.backgroundColor='white';
  13.         obj.style.color='#FF0000';
  14.         obj.style.border='1px solid #6D6E71';
  15.     }
  16. }
Disclaimer: I realize this has compat issues - assume IE for now. If you have a good reference for cross-browser, please please link me.


EDIT: obj.innerHTML.s tyle.color has the same effect
Also, obj.style.color works for non-link text
Feb 19 '08 #1
3 2023
gits
5,390 Recognized Expert Moderator Expert
hi ...

use the following function to set the color:

Expand|Select|Wrap|Line Numbers
  1. function set_color(obj, color) {
  2.     var n = obj.childNodes;
  3.  
  4.     for (var i = 0, node; node = n[i]; i++) {
  5.         node.style.color = color;
  6.     }
  7. }
  8.  
kind regards
Feb 19 '08 #2
isoquin
48 New Member
hi ...

use the following function to set the color:

Expand|Select|Wrap|Line Numbers
  1. function set_color(obj, color) {
  2.     var n = obj.childNodes;
  3.  
  4.     for (var i = 0, node; node = n[i]; i++) {
  5.         node.style.color = color;
  6.     }
  7. }
  8.  
kind regards
thank you much dear, that did the trick.

If you don't mind a quick followup (as I like to know the code, not just copy it), does this simply traverse every element in the div and individually assign the color?
Feb 21 '08 #3
gits
5,390 Recognized Expert Moderator Expert
thank you much dear, that did the trick.

If you don't mind a quick followup (as I like to know the code, not just copy it), does this simply traverse every element in the div and individually assign the color?
exactly - but only the 'direct' childs ... what is enough for our purpose ;) ... post back in case you have more questions ...

kind regards
Feb 21 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
7060
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass simple/advanced html forms and how it can be used to override cookie/session
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.