0

Hi I have a website template I can't change since it's server side of a company I'm partnered with, but I want to change this code to remove onload this td background image or change it, whatever works. It needs to be with javascript not jquery.

It has no id or css

<td background="/webfiles/302565/free/b2.gif">

I tried something like this but it doesn't work.

var tabletd = document.getElementsByTagName('td');

if (td.background == 'b2.gif') {
    td.background = 'b-2.gif';
   }

A million thank you's in advance!

*EDIT: Thanks for the help and code, however although it works in jsfiddle it doesn't work on my site (www.skiptheframe.tk). I have other working replace scripts but this td background one has been a thorn in my side for ages since I have tried many variations to try get it to work.

  • 1
    `document.getElementsByTagName()` returns a list, so you need a loop. (I'm all for omitting irrelevant code in a question, but if you have a loop that sets the `td` variable that is relevant and you should [edit] your question to show it.) – nnnnnn Jul 01 '16 at 06:48
  • 1
    Also, `td.background` is invalid, it's `td.style.background`. – Arg0n Jul 01 '16 at 06:49
  • See code here: https://jsfiddle.net/Arg0n/uwtanp3y/ Was gonna post answer but someone closed. – Arg0n Jul 01 '16 at 06:57
  • thanks guys, so like this? var tabletd = document.getElementsByTagName('td'); for (var i=0; i – General Noob Jul 01 '16 at 06:57
  • Acctually, since you are using an attribute for the background, use this: `td.attributes.background.value = 'your image';`. – Arg0n Jul 01 '16 at 07:00
  • Also, for a general replace (instead of one for each), you can use this: `td.attributes.background.value.replace(/b(\d)\.gif/g, "b-$1.gif");` – Arg0n Jul 01 '16 at 07:02
  • Everything put together here: https://jsfiddle.net/Arg0n/uwtanp3y/1/ – Arg0n Jul 01 '16 at 07:04
  • sorry to be a pain but i'm probably not doing it right, would it possible to have the complete code to copy? – General Noob Jul 01 '16 at 07:05
  • thanks arg0n - can't believe you are from Jonkoping! I lived there for a few years, favourite place ever lol – General Noob Jul 01 '16 at 07:06
  • but alas it still doesn't work :( – General Noob Jul 01 '16 at 07:12

0 Answers0