Referencing Multiple Images with the Same Name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucanus

    #1

    Referencing Multiple Images with the Same Name

    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.pictu re[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
  • Lasse Reichstein Nielsen

    #2
    Re: Referencing Multiple Images with the Same Name

    isolve@hotmail. com (Lucanus) writes:
    [color=blue]
    > Can I have a document with three images with the same name, and
    > reference those images individually.[/color]

    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
    [color=blue]
    > 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.pictu re[2].src' and retrieve
    > 'file3.jpg'?[/color]

    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
    [color=blue]
    > I have tried this without success, and am hoping that you can educate
    > me on whether this is just poor syntax on my part,[/color]

    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.getEle mentsByName("pi cture")).

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...