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 7 of 7

Thread: Images object

  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Images object

    good day...
    i'm trying to access a certain image object without using IDs...
    document.images array allows me to do it...

    but is there a way for an image to identify its own number....given that there are 3 images, can the second image know that it is image 2?

    <img onclick=alert(this.image.number)>

    something like that...

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by ken_shoti View Post
    good day...
    i'm trying to access a certain image object without using IDs...
    document.images array allows me to do it...

    but is there a way for an image to identify its own number....given that there are 3 images, can the second image know that it is image 2?

    something like that...
    Untested, but try it:
    Code:
    <img onclick="alert(getImageIndex(this))">
    .
    .
    function getImageIndex(ref)
    {
     for(var i=0,len=document.images.length; i<len && ref!=document.images[i]; i++)
     ;
     return i;
    }

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    nice...it works...but looping seems not a good choice esp. if i have lots of images....

    added to that: is there a solution to access images in a particular area/tag-enclosed...

    <img src=img.gif>
    <div id=imgdiv>
    <img src=img.gif>
    </div>

    can i get access only to the second image?

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    To access the second image on a page
    Code:
    document.images[1]
    just like in the loop

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hm..no i mean something...

    so document.images will not anymore 1 but 0... is there a way to focus only images on a certain div area and not the entire page...?

  • #6
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by ken_shoti View Post
    hm..no i mean something...

    so document.images will not anymore 1 but 0... is there a way to focus only images on a certain div area and not the entire page...?
    Perhaps
    Code:
    var divImages=document.getElementById('myElem').getElementsByTagName('img');
    but I think you still have to loop to determine position.

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hm...will try, but i think looping can be faster? or will it? haha...thanks i will try it in the future


  •  

    Posting Permissions

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