ken_shoti
05-16-2007, 02:41 AM
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...
Arty Effem
05-16-2007, 03:09 AM
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:
<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;
}
ken_shoti
05-16-2007, 03:46 AM
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?
rwedge
05-16-2007, 04:06 AM
To access the second image on a page
document.images[1]
just like in the loop
ken_shoti
05-16-2007, 04:22 AM
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...?
Arty Effem
05-16-2007, 09:55 AM
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...?
Perhapsvar divImages=document.getElementById('myElem').getElementsByTagName('img');but I think you still have to loop to determine position.
ken_shoti
05-24-2007, 12:42 PM
hm...will try, but i think looping can be faster? or will it? haha...thanks i will try it in the future