...

View Full Version : some js help please



sted
04-23-2008, 01:01 PM
Hi, I need some help changing a background color using javascript...
The HTML Code has this structure:


<table>
<table>
<form>
<tr>
<td bgcolor="#000000"></td>
</tr>
<tr>
<td bgcolor="#000000"></td>
</tr>
<tr>
<td bgcolor="#000000"></td>
</tr>
.
.
.
</form>
</table>
</table>

Now the tricky part is that I CANNOT alter the HTML code, I can't give the elements ID's or NAME's, is it still possible to change bgcolor via javascript?
Can someone provide me a script template for changing all the td bgcolors to a specific color, like black?

abduraooft
04-23-2008, 01:06 PM
var tds=document.getElementsByTagName('td');

for(var i=0;i<tds.length;i++)
tds[i].style.backgroundColor='green';

shibu
05-02-2008, 09:05 AM
<table>
<table>
<form>
<tr>
<td id="cell1"></td>
</tr>
<tr>
<td id="cell2"></td>
</tr>
<tr>
<td id="cell3"></td>
</tr>
.
.
.
</form>
</table>
</table>

<script>
function hilite(id) {

document.getElementById("cell1").style.backgroundColor = "red";
document.getElementById("cell2").style.backgroundColor = "black";
document.getElementById("cell3").style.backgroundColor = "green";

}
</script>

http://www.outsource-website-development.com/ http://www.outsource-website-design.com/

shibu
05-02-2008, 10:15 AM
<table>
<table>
<form>
<tr>
<td id="cell1"></td>
</tr>
<tr>
<td id="cell2"></td>
</tr>
<tr>
<td id="cell3"></td>
</tr>
.
.
.
</form>
</table>
</table>

<script>
function hilite(id) {

document.getElementById("cell1").style.backgroundColor = "red";
document.getElementById("cell2").style.backgroundColor = "black";
document.getElementById("cell3").style.backgroundColor = "green";

}
</script>

shibu
05-02-2008, 10:55 AM
This one should be correct

<script language="javascript">

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}

addLoadEvent(function() {
var tds=document.getElementsByTagName('td');
for(var i=0;i<tds.length;i++)
tds[i].style.backgroundColor='green';
});

</script>

<table>
<table>
<form>
<tr>
<td bgcolor="#000000"></td>
</tr>
<tr>
<td bgcolor="#000000"></td>
</tr>
<tr>
<td bgcolor="#000000">
</td>
</tr>
</form>
</table>
</table>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum