By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

Referencing Multiple Images with the Same Name

P: n/a
Hi All,

Just a quick question, which should have a simple answer...

Can I have a document with three images with the same name, and
reference those images individually.

ie:
<img name="picture" src="file1.jpg">
<img name="picture" src="file2.jpg">
<img name="picture" src="file3.jpg">

Would I be able to refer to 'document.picture[2].src' and retrieve
'file3.jpg'?

I have tried this without success, and am hoping that you can educate
me on whether this is just poor syntax on my part, or whether I am
simply expecting too much of JavaScript...

Thanks
Luke
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
is****@hotmail.com (Lucanus) writes:
Can I have a document with three images with the same name, and
reference those images individually.
Why give them a name if they are all the same?
Give them different names instead, or preferably, different ids.
Why not
<img id="picture1" src="file1.jpg">
<img id="picture2" src="file2.jpg">
<img id="picture3" src="file3.jpg">
and
document.images['picture2'].src
ie:
<img name="picture" src="file1.jpg">
<img name="picture" src="file2.jpg">
<img name="picture" src="file3.jpg">

Would I be able to refer to 'document.picture[2].src' and retrieve
'file3.jpg'?
That depends on the browser, and most likely not.
What you can do, if those three images are the only ones on the page, is
document.images[2].src

In most browsers, you should be able to do:
document.images['picture'][2].src
I have tried this without success, and am hoping that you can educate
me on whether this is just poor syntax on my part,


Not syntax, but the access method. You write "document.<name of
element>". That is not a good way to access an element. Instead you
should go through either one of the collections (like
document.images['picture']) or use another method to access elements
(like document.getElementsByName("picture")).

On top of that is the problem of having more than one element with the
same name, which should generally be avoided outside of forms.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.