Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 31st, 2004, 06:57 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get stylesheet's background color

I have a html page which include a stylesheet. I defined my stylesheet as below :

[code]

/* style.css */

tr.tblrow td {
   background-color: #def;
   color: #000;
   padding: 0px 5px;
}

[code]

And in my html page, I have a javascript function which will alert me about the background-color as well as font color.

Code:
<html>
<head>
<title>Bg Color</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script language="javascript" type="text/javascript">

function getColor(obj) {
   var el = event.srcElement;
   var txtColor = el.style.color;
   var bgColor = el.style.backgroundColor;

  alert("txtColor = " + txtColor);
  alert("bgColor = " + bgColor);
}

</script>
</head>

<form name="form">
<table>
   <tr class="tblrow" onClick="getColor(this); return false">
      <td>Student ID</td>
      <td>Student Name</td>
   </tr>
</table>
</form>
</html>
when I clicked on it, I get both color and background-color equal to blank. But, it suppose to be "txtColor = #000" and "bgColor = #def" .

Could anybody help to correct my mistake(s)? Thanks.

  #2 (permalink)  
Old October 31st, 2004, 07:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If in IE (don't know about others) then try currentStyle or runtimeStyle instead of style.

--

Joe
  #3 (permalink)  
Old October 31st, 2004, 08:03 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks joefawcett, it works.



Similar Threads
Thread Thread Starter Forum Replies Last Post
change color of background meena88 C# 2005 3 December 1st, 2009 04:48 AM
Background Color matrix ArnieStewart BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 3 October 21st, 2008 01:12 AM
mdi-background color avats General .NET 1 February 28th, 2008 10:25 AM
background-color disappears harpua CSS Cascading Style Sheets 1 August 19th, 2005 04:53 PM
Status Bar Background Color kumar_garg78 Beginning VB 6 1 August 30th, 2004 11:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.