/    Sign up×
Community /Pin to ProfileBookmark

reading backgroundColor of an element

This:

[code=php]alert(document.getElementById(‘cell_1’).style.backgroundColor);[/code]

is just returning a blank. Is it not possible to read style from an element in this fashion? Here’s the rest of the script:

[code=php]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<style type=”text/css”>
table{
border: 1px solid #000;
}

td{
width: 80px;
height: 100px;
border: 1px solid #000;
}

#cell_1{
background-color: #0f0
}

#cell_2{
background-color: #00f
}

#cell_3{
background-color: #c0f
}

#cell_4{
background-color: #f92
}

#cell_5{
background-color: #f00
}

#cell_6{
background-color: #ff0
}

#cell_7{
background-color:
}

</style>
<title>Untitled</title>
</head>
<body>
<table>
<tr>
<td id=”cell_1″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_2″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_3″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_4″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_5″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_6″ onclick=”move(this.id);”>&nbsp;</td>
<td id=”cell_7″ onclick=”move(this.id);”>&nbsp;</td>
</tr>
</table>

<script type=”text/javascript”>
alert(document.getElementById(‘cell_1’).style.backgroundColor);
</script>

</body>
</html>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@saulssOct 28.2005 — i have no clue, but this is an alternative:
[code=html]<div id="asdf" style="background-color:red;">test</div>
<script>
alert(document.getElementById('asdf').style.backgroundColor)
</script>[/code]
Copy linkTweet thisAlerts:
@cootheadOct 28.2005 — Hi there bokeh,

try it like this.....
[color=navy]&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
[color=purple]
/****************************

Note:- This script is by lioran not coothead.

****************************/[/color]
function test(id){
var el = document.getElementById(id);

<i> </i>if(document.defaultView &amp;&amp; document.defaultView.getComputedStyle) {
// IS MOZILLA BASED BROWSER //;
var bg = document.defaultView.getComputedStyle(el, '').getPropertyValue("background-color");

<i> </i> alert('DOM way: '+bg);
<i> </i>}

<i> </i>else if(document.uniqueID &amp;&amp; el.currentStyle){ // IS INTERNET EXPLORER //;
<i> </i> alert('IE way: '+el.currentStyle.backgroundColor);
<i> </i>}
}
//--&gt;
&lt;/script&gt;

&lt;style type="text/css"&gt;
table{
border: 1px solid #000;
}
td{
width: 80px;
height: 100px;
border: 1px solid #000;
}
#cell_1{
background-color: #0f0
}
#cell_2{
background-color: #00f
}
#cell_3{
background-color: #c0f
}
#cell_4{
background-color: #f92
}
#cell_5{
background-color: #f00
}
#cell_6{
background-color: #ff0
}
#cell_7{
background-color:
}

&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;table&gt;&lt;tr&gt;
&lt;td id="cell_1" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_2" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_3" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_4" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_5" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_6" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td id="cell_7" onclick="test(this.id)"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt; [/color]


[b]Note[/b]:- this script is by [b]lioran[/b] not...

[i]coothead[/i]
×

Success!

Help @bokeh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...