PDA

View Full Version : td background



lord22
08-23-2009, 08:22 AM
Hi,

How can I get td's background-color and write it on a specific text field, while clicking on it?

Thank you

coothead
08-23-2009, 11:19 AM
Hi there lord22,

have a look at this example it may help...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
#mytable {
border:2px solid #999;
margin:auto;
}
#mytable td {
width:100px;
height:62px;
border:1px solid #666;
text-align:center;
cursor:pointer;
}
#td1 {
background-color:#fcc;
}
#td2 {
background-color:#cfc;
}
#td3 {
background-color:#ccf;
}
#color {
display:block;
width:320px;
border:3px double #999;
margin:10px auto;
text-align:center;
}
</style>

<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',mytest,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',mytest);
}
}

function mytest(){
tds=document.getElementById('mytable').getElementsByTagName('td');
for(c=0;c<tds.length;c++) {
tds[c].onclick=function() {
if(this.currentStyle) {
bgColor=this.currentStyle.backgroundColor;
document.getElementById('color').value=
'td id="'+this.id+'" background-color="'+bgColor+'"';
}
else{
compStyle=getComputedStyle(this,'');
bgColor=compStyle.getPropertyValue('background-color');
document.getElementById('color').value=
'td id="'+this.id+'" background-color="'+bgColor+'"';
}
}
}
}
</script>

</head>
<body>

<table id="mytable"><tr>
<td id="td1">one</td>
<td id="td2">two</td>
<td id="td3">three</td>
</tr></table>

<div>
<input id="color" type="text">
</div>

</body>
</html>

coothead

lord22
08-23-2009, 08:23 PM
Thank you!

coothead
08-23-2009, 08:57 PM
No problem, you're very welcome. :)