0

Can anyone tell me how I can access the following line of HTML in JavaScript, please? I cannot seem to find the image object in JavaScript:

<td colspan="2" rowspan="5"><img src="../image/6765.jpg" width="330" height="130" />

The end result is that I want to change the images. Thanks for any help !

2 Answers 2

1

You should give your <img> element a unique id:

<img id="myImage" src="../image/6765.jpg" width="330" height="130" />

Then in JavaScript you should be able to get a reference to your element with document.getElementById():

document.getElementById('myImage').src = 'new-image.jpg';
Sign up to request clarification or add additional context in comments.

Comments

0

or ... hunt for it's occurrences (0,1,2 or more x) ...

javascript:
    for(i in document.images)
        if(/\/image\/6765\.jpg/.test(document.images[i].src))
/* resolve the base address with the '..' for a more stringent test */
             alert([ 'image URI found at image index: ', i,
                      '\n\nimage.src is\n', document.images[i].src ])

For instance looking for '.jpg' or '.png' or '.gif' on this page itself

javascript:
    for(i in document.images)
        if(/\.(jpg|gif|png)/.test(document.images[i].src))
             alert([ 'image URI found at image index: ', i,
                      '\n\nimage.src is\n', document.images[i].src ])

finds:

image URI found at image index: ,0,

image.src is
,http://static.adzerk.net/Advertisers/c9180f024ecb4880954922ef7a72be87.gif  
 
 
 
image URI found at image index: ,4,

image.src is
,http://static.adzerk.net/Advertisers/2568.jpg  
 
 
 
image URI found at image index: ,6,

image.src is
,http://sstatic.net/ads/img/careers2-ad-header-so.png

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.