473,508 Members | 2,357 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.style.color has the same effect
Also, obj.style.color works for non-link text
Feb 19 '08 #1
3 2014
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
7054
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...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
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...
1
7039
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...
0
7494
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...
0
5626
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,...
1
5050
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...
0
4706
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...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.