How to access the background color of a div element ?

Y

Yashesh Bhatia

Hi

Sorry if it's a trivial question. I'm a newbie to js and did some
reading since the last 3-4 hrs but have not been able to get the
answers, hence this post.

essentially i'm trying to access the bacground color of a div which i
need to use in javascript.

here are my files

------------------- test1.html --------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test1</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<SCRIPT type="text/javascript" src="test1.js"></script>
<LINK href="test1.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
window.onload = f1;
</script>
</head>
<body>
<div id="div1">
<h1>Communities</h1>
Indus Valley Civilization<p>
Walt Disney<p>
Goa as a tourist destination ?<p>
Programming in Javascript & CSS ?<p>
World is Flat<p>
</div>
<div id="div2">
<h1>Events</h1>
Down but not out ?<p>
Car rally down south<p>
5k AIDS awareness run<p>
go for the gold<p>
Introspection<p>
</div>
</body>
</html>
-----------------------------------------------------------------------
------------------- test1.css --------------------------------------
#div1 {
background-color:#FF0000;
}

#div2 {
background-color:#00FFFF;
}
-----------------------------------------------------------------------
------------------- test1.js ----------------------------------------
function f1() {
alert("Entering function f1");

var div1 = document.getElementById('div1');
for(property in div1) {
//alert(property + ' = ' + div1.property);
}

var div1_bg_color = div1.style.backgroundColor;
alert("div1 = " + div1);
alert("div1_bg_color = " + div1_bg_color);

}
-----------------------------------------------------------------------

Now when i run the above script i get the following relevant popups
div1 = [object HTMLDivElement]
div1_bg_color =

My question is how can i access the background color of div1 in
javascript and store it as a string variable ? I'n using Firefox 1.5

Thanks for assistance.

Yashesh Bhatia.
 
P

Private Cow of Funk

Oooh! Ooh! I can answer this one! Let me! Let me!!!

The object is referenced thusly:

document.getElementById( 'the_id_of_your_div' ).style.backgroundColor

In a basic function it would be;

function the_crunge()
{
document.getElementById(
'wheres_that_confounded_bridge' ).style.backgroundColor="#000000";
}

to make the background colour black. I hope it will be useful.

John Bonham
 
M

Matt Kruse

Private said:
Oooh! Ooh! I can answer this one! Let me! Let me!!!

But your answer isn't correct :)
The object is referenced thusly:
document.getElementById( 'the_id_of_your_div' ).style.backgroundColor

Only if the background color was set explicitly in the style attribute of
the object.
Plus, isn't this exactly what the OP posted, which didn't work?

The answer is, there are a couple different things you need to check to get
the right answer, since the background color may have been set by inline
styles, a class, or because of some other css rule. What you really need to
know is what bg color is currently _applied_ to the object.

For this, see my simple wrapper:

CSS.getStyle(obj, "background-color");

which can be found here, along with my other "util" functions:

http://www.JavascriptToolbox.com/lib/util/

The benefit of a general wrapper function is that it will normalize
differences in browsers and handle special cases like "float" and some
browsers returning "red" while others return "#ff0000".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top