473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to set background of TD without an ID to none

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;"
</td>


<td style="background-image: url(image2.gif);background-repeat:
repeat-x;"></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.

Is there a way to target TD's, maybe even these specific TD's using
nodes (if I understand nodes at all...).

cheers, Kor

Aug 2 '05 #1
5 11057
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;">
&nbsp;</td>
<td style="background-image: url(a.gif);
background-repeat: repeat-x;">&nbsp;</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
Aug 3 '05 #2
Hi Rob,

Thanks a million, it works, I am so happy. I also analysed the code and
I think I understand it.

When you say 'doesn't work' you should specify what you tried, what you
expected to happen and then what actually did happen.


-What I tried was a.o.:

document.TableData.background=none;
(http://www.w3schools.com/htmldom/dom_obj_tabledata.asp)
and
document.getElementsByTagName('td').background="ht tp://www.someplace.com/image.gif";

etc etc, played with quotes, double quotes etc.

-What I expected to happen was to not see a background picture in the
table cell, but nothing instead, or second option, a transparent gif
instead;

-What happened was just nothing, the images were still visible.
But not anymore

I tried to extend your code, can you maybe tell me (if you have the
time)if this make sense?: (it doesnt work, but I suspect that a
document.write which does something with images messes it up, or maybe
the code is just wrong)
function removeTdBackground( el ) {
if ( !document.getElementsByTagName ) return;

var cell, happyimage, cells = el.getElementsByTagName('td'),
happyimages = el.getElementsByTagName('img');
if ( !cells.length || !cells[0].style || !happyimages.length ||
!happyimages[0].style) return;

var i = cells.length;
while ( i-- ) {
cell = cells[i];
cell.style.backgroundImage = '';
cell.style.background = '';
cell.background = '';
cell.setAttribute('background', '');
}

var j = happyimages.length;
while ( j-- ) {
happyimage = happyimages[i];
happyimage.scr = 'http://www.someplace.com/image.gif';
//maybe the .scr is not right

}
}
Thanks again!
Kor

Aug 3 '05 #3
happyimage.scr = 'http://www.someplace.com/image .gif';

is not correct

happyimage.src = 'http://www.someplace.com/image .gif';

is what it should be.

So you were correct about that that line of code was wrong.

Aug 3 '05 #4
pr******@operamail.com wrote:
Hi Rob,

Thanks a million, it works, I am so happy. I also analysed the code and
I think I understand it.
When you say 'doesn't work' you should specify what you tried, what you
expected to happen and then what actually did happen.

-What I tried was a.o.:

document.TableData.background=none;


You can't use that syntax for accessing a table. The collections that
you can access using HTMLdocument (i.e. document.<something>) are
anchors, applets, images, forms and links.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268>

To get a reference to the 2nd form in the document you can use:

var formTwo = document.forms[1]; // Note they are numbered from 0

Supposing that your table has an id of 'TableData', then you can get a
reference to it with getElementById:

var table = document.getElementById('TableData');

The table element does not have a background attribute: the body element
does but it's depreciated, so don't use it at all, use CSS.

<URL:http://www.w3.org/TR/html4/struct/global.html#adef-background>

If you want to modify any property of any element, you need to use an
appropriate value. Supposing you want to modify the body background
attribute, it requires a URI which must be a string. So whatever is
assigned to the document.body.background attribute must be a string (or
something that resolves to a string).

String literals in JavaScript must be quoted, otherwise the script
engine will try to evaluate it (and possibly fail or do something
unexpected). If you want to remove the value in the body's background
attribute, then set it to an empty string:

document.body.background = '';

Otherwise it should be a valid URI.
(http://www.w3schools.com/htmldom/dom_obj_tabledata.asp)
That link tells you that the table background property is IE only and is
not part of the W3C HTML 4 specification.
and
document.getElementsByTagName('td').background="ht tp://www.someplace.com/image.gif";
document.getElementsByTagName() will return a collection (like an array
but a bit different - it implements the nodeList interface which has one
property 'length' and one method 'index') that contains all the TD
elements in the document. It does not have a background property - you
may be able to give it one but it won't know what to do with it.

getElementsByTagName:
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-A6C9094>

Interface nodeList
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-536297177>
etc etc, played with quotes, double quotes etc.
Using double & single quotes can be a bit confusing, have a read of
Peter-Paul Koch's page on strings:

<URL:http://www.quirksmode.org/js/strings.html>

-What I expected to happen was to not see a background picture in the
table cell, but nothing instead, or second option, a transparent gif
instead;

-What happened was just nothing, the images were still visible.
But not anymore

I tried to extend your code, can you maybe tell me (if you have the
time)if this make sense?: (it doesnt work, but I suspect that a
document.write which does something with images messes it up, or maybe
the code is just wrong)
Without seeing the document.write statement it's hard to tell ;-) but
generally whatever it writes is equivalent to the same thing in the HTML
source.


function removeTdBackground( el ) {
if ( !document.getElementsByTagName ) return;
This line tests if the browser supports getElementsByTagName and exits
cleanly if it doesn't - the vast majority of browsers do, but there are
a few that may still fail.

var cell, happyimage, cells = el.getElementsByTagName('td'),
happyimages = el.getElementsByTagName('img');
if ( !cells.length || !cells[0].style || !happyimages.length ||
!happyimages[0].style) return;
You only need to test for support for the style object once, so the
second one can go. The images collection already exists, you don't need
to use getElementsByTagName, so you could write:

happyimages = document.images;

which should be a lot faster and the test would be:

if ( !cells.length || !cells[0].style || !happyimages.length ) return;

var i = cells.length;
while ( i-- ) {
cell = cells[i];
cell.style.backgroundImage = '';
cell.style.background = '';
cell.background = '';
cell.setAttribute('background', '');
}

var j = happyimages.length;
while ( j-- ) {
happyimage = happyimages[i];
Storing a reference to a collection item is useful if you are going to
use it multiple times (for speed and neater), but for one-of it's
suitable to not bother.

happyimages[i].src = 'http://www.someplace.com/image.gif';
In that vein, it's also suitable to use a short variable name (if you
are going to keep a reference):

hi = happyimages[i];
hi.src = 'http://www.someplace.com/image.gif';
happyimage.scr = 'http://www.someplace.com/image.gif';
//maybe the .scr is not right


The IMG element does not have an 'scr' attribute, I think you're looking
for 'src'.

<URL:http://www.w3.org/TR/html4/struct/objects.html#adef-src-IMG>

Otherwise, the code looks fine. Just note that using while as above
will go through the collections backwards - which is sometimes faster
but may be slower on some platforms in some contexts and is occasionally
inconvenient. For small collections (say less than 50 items) speed is
likely totally irrelevant.

I prefer to code that way, a 'forward looping' alternative would be:

for ( var i=0, j=happyimages.length; i<j; i++ ) {
//...
}

[...]

--
Rob
Aug 3 '05 #5
Rob,
Thank you very much. A lot to chew on, a lot to learn. I am going to
try this.
-Kor

Aug 5 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shannon | last post by:
Hi there, I have an HTML page with one large background image in the left corner. I've got various links positioned randomly on the image, and I use <span> tags to display text on another...
7
by: Thomas Wieser | last post by:
Hi, my problem: I have some tables with transparent backgrounds, which are changed in colours within a JavaScript DOM function to have a roll-over effect. Now, i can't get them back...
23
by: Erik Schulp | last post by:
Hi all, I am using a background image via a stylsheet. I've used this code: background-image:url("/images/tile.gif"); (which I think is correct) The image doesn't show up however, the path,...
1
by: CMAR | last post by:
I have a design of a frameless page on my practice website: http://home.ne.rr.com/thespar/designerN.htm The idea is to have a #left navigation bar which is absolutely positioned and which...
13
by: Giggle Girl | last post by:
Hi there, I need to use a background image in a TR that does NOT restart everytime it hits a TD. Can it be done? Specifically, if you set a background image for an entier table, now mater how...
2
r0g
by: r0g | last post by:
It seems w3 don't want us applying widths to our inline elements, I can't see why not and indeed if you throw in the 'position: absolute' property (without any 'top' 'bottom' etc) Firefox, Opera & to...
16
by: stevedude | last post by:
CSS newbie again. I have a problem trying to get coffee mug images within anchor tags to center with my link text for a vertical list menu. If I use the horizontal/vertical properties of...
2
by: patrick.waldo | last post by:
Hi all, I am trying to figure out a way to read colors with xlrd, but I did not understand the formatting.py module. Basically, I want to sort rows that are red or green. My initial attempt...
10
by: VividWeb | last post by:
Hi. I am relatively new to CSS and HTML but have a basic understanding of most things. One of my backgrounds is not positioning correctly in IE 7 or AOL. The background behind the content...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.