Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    108
    Thanks
    22
    Thanked 2 Times in 2 Posts

    images have the same name value

    if the elements have name attributes with the same value, according to legacy DOM, it will return an array of those elements if you access the name property of 'document'.

    However, this doesn't apply to img elements. If the HTML code is as follows:
    Code:
    <img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
    <form name="pic">a</form>
    <form name="pic">b</form>
    then
    document.forms["pic"] (return an array) <-- as expected
    document["pic"] or document.pic (return an array) <-- as expected
    document.images["pic"] :
    in IE -- it returns the last image
    in FF -- it returns the first image
    ~~OMG~~


    any idea?

    thx in advance.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    AFAIK that applies only to forms and their elements.
    document.images[ 'duplicate' ] can be interpreted any way, so there is no unexpected behaviour there.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,534
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
    <form name="pic">a</form>
    <form name="pic">b</form>
    <script  type="text/javascript">
    /*<![CDATA[*/
    alert(document.getElementsByName('pic').length);
    /*]]>*/
    </script></body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Posts
    108
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Arty Effem View Post
    document.images[ 'duplicate' ] can be interpreted any way, so there is no unexpected behaviour there.
    but it should return an array of those images instead of one single image~~~~


    @vwphillips haha this can be a workaround but still need to be filtered with 'img' tag coz i want images with that name.

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by mehere8 View Post
    but it should return an array of those images instead of one single image~~~~
    Is there a specification stipulating that?
    Do you know that the name attribute is deprecated except for elements of forms?
    Last edited by Arty Effem; 08-27-2010 at 07:19 PM.

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,534
    Thanks
    3
    Thanked 513 Times in 500 Posts
    document.getElementsByTagName( 'img' ).getElementsByName( 'pic' );
    will result in an error

    use post#3

    or
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
    <form name="pic">a</form>
    <form name="pic">b</form>
    <script  type="text/javascript">
    /*<![CDATA[*/
    var imgs=document.getElementsByTagName( 'img' )
     for (var ary=[],z0=0;z0<imgs.length;z0++){
      if (imgs[z0].name&&imgs[z0].name=='pic'&&imgs[z0].nodeName.toUpperCase()=='IMG'){
       ary.push(imgs[z0]);
      }
     }
    alert(ary.length);
    /*]]>*/
    </script></body>
    
    </html>
    Last edited by vwphillips; 08-27-2010 at 06:04 PM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •