pr******@operamail.com wrote:
Hi,
I am trying to change the background of table TD's. The problem is that
I have no access to the HTML code. SO I am trying to alter this using
Javascript/DOM in an external .js file.
I have tried lot's of things and spent hours on this, I thought I might
give it a try here. One of the TD's uses HTML to set the background,
the other one uses CSS styling:
<td background="image1.gif" style="background-repeat: repeat-x;"
There is no attribute 'background' for TD elements in the HTML 4
specification. There is a depreciated background attribute for the BODY
element, but it should never be used on a TD.
However, if a background attribute is coded in a TD's HTML source, both
IE and Firefox will display it - which is one of the reasons why support
for invalid markup is a contentious issue. Removal of the invalid
attribute is shown below.
</td>
<td style="background-image: url(image2.gif);background-repeat:
repeat-x;"></td>
That is one of the valid ways to add a background image to a TD.
As you can see the TD's do not have an ID.
I tried to use getElementsByTagName, but it doesnt seem to work. I am
not a programmer unfortunately.
When you say 'doesn't work' you should specify what you tried, what you
expected to happen and then what actually did happen.
Is there a way to target TD's, maybe even these specific TD's using
nodes (if I understand nodes at all...).
Yes, but the effort you spend on sniffing out the subject TD is directly
proportional to the fragility of the code. So here is a very general
method which will hopefully not do too much more damage than you can
tolerate.
It supposes that the table has an ID - if not, you will have to feed it
all the TDs in the document.
<script type="text/javascript">
function removeTdBackground( el ) {
if ( !document.getElementsByTagName ) return;
var cell, cells = el.getElementsByTagName('td');
if ( !cells.length || !cells[0].style ) return;
var i = cells.length;
while ( i-- ) {
cell = cells[i];
cell.style.backgroundColor = '';
cell.style.backgroundImage = '';
cell.style.background = '';
cell.background = '';
cell.setAttribute('background', '');
// anything else while we're here....
}
}
</script>
<table id="tableA" width="300">
<tr>
<td background="a.gif" style="background-repeat: repeat-x;">
</td>
<td style="background-image: url(a.gif);
background-repeat: repeat-x;"> </td>
</tr>
</table>
<input type="button" value="Remove cell styles" onclick="
removeTdBackground( document.getElementById('tableA') );
">
I should note that normally you can access the properties of an element
directly, so removing the background using the following should work:
cell.background = '';
That seems to work in IE, but not in Firefox hence I've used
setAttribute which appears to work in both. You may want to test more
widely and maybe use both remove statements (it isn't harmful to
Firefox, just ineffective).
To apply the script to every TD in the document, change the call to:
removeTdBackground( document.body );
Note that either getElementsByTagName or the style object may not be
supported by some older browsers.
--
Rob