How can I reference this element using document in Javascript

supportoranges
supportoranges used Ask the Experts™
on
I need to dynamically change the src attribute (see snippet below).  Please tell me how can I reference this using document in Javascript.  I have limited Javascript exp.

<td width="150" height="198"><img border=0 width=150 height=76 src=http://ep.yimg.com/ca/I/yhst-123908713209812348></td>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kim WalkerWeb Programmer/Technician

Commented:
Can you modify the html? If the img tag had an id attribute, you could reference it by its id. For example:

<img id=myimage" border="0" width="150" height="76" src="http://ep.yimg.com/ca/I/yhst-123908713209812348">

Open in new window

could be referenced in javascript by:

document.getElementById("myimage").src = "http://newurl";

Open in new window

Author

Commented:
I cannot add id attribute on the img tag but i can add it on the <td>

THIS IS POSSIBLE:
 <td id="cellContainingImage" width="150" height="198"><img border=0 width=150 height=76 src=http://ep.yimg.com/ca/I/yhst-1234567890></td>

The reason I can't add id attribute on img tag is because html for <img> tag is generated using Yahoo store tag.
Web Programmer/Technician
Commented:
Are there other images in that same td? You can use getElementsByTagName to capture all the images in that td and then reference them by index. If there's only one, it would be the first reference. If there are others, you'd need to know which one this is.

var myimages = document.getElementById('cellContainingImage').getElementsByTagName('img');
myimages[0].src = "http://newurl";

Open in new window

Author

Commented:
That will work - lovely!

Author

Commented:
Thank you, that was very helpful

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial