tags:

views:

41

answers:

3

Hello friends! I have 5 images on my page. These images are layered on each other using Z-index in css class. Out of these 5 images I have assigned a css class to 4 images and for one image i.e. first image img1 I have added css effect using style property in image tag only. Now out of these 5 images first image img1 is always visible and out of remaining 4 images only 1 image is visible and other three images remain hidden. These 4 images I have assigned a css class. Question I have 3 buttons on page and each having different purpose and function. I just want to know that which 2 images are visible i.e. shown (not hidden) on page when any of these three button is clicked using Jquery.

A rough HTML code is as follows to get brief idea

<html>
<head>
</head>
<body>

<input type="button" id="button_1" name="zoomin" value="Zoom In" tabindex="18">
<input type="button" id="button_2" name="zoomout" value="Zoom Out" tabindex="19">
<input type="button" id="button_3" name="original" value="Original" tabindex="20">

<div id="div1" class="test1" >
    <img src="images/base.jpg" style="z-index:1; position:absolute; top:0; left:0; width:550; height:750">
    <img src="images/yellow_refno.gif" class="classforimg">
    <img src="images/yellow_title.gif" class="classforimg">
    <img src="images/yellow_gender.gif" class="classforimg">
    <img src="images/yellow_gender.gif" class="classforimg">          
</div>
</body>
</html>

So friends please help me! Thank You!

+4  A: 

The jQuery :visible selector should suit your needs

$('img:visible')
Simon
+2  A: 

$('#div1 img:visible')

This will return a collection of visible images under #div1

As Andreas Niedermair mentioned, there is a difference between:

$('#div1 img:visible')

which selects all image descendants and:

$('#div1 > img:visible')

which selects immediate decendants only.

The difference is not significantly relevant to the code example given.

patrick dw
there's a difference in between `'#div1 img:visible'` and `'#div1 > img:visible'` selector, which should be mentioned!
Andreas Niedermair
@Andreas: yes, and what is it?!
Milan Babuškov
@Andreas - A technicality of little significance given the code. Not sure why I would have a vote removed by someone.
patrick dw
+1 to counter..
Blair McMillan
Rock on, Blair! :o)
patrick dw
A: 

The question is how do you hide/show the images. If you use the hide(),show() methods to toggle the display property, then you can use:

$("img:visible").attr("src")

to find out which image is possible. It will be better to assign ids in the images, so that you don't need to rely on the src attribute.

If you modify the z-index, then you need to read the z-index of all images to find, which one is on top of the others. You can do that with the css() method.

kgiannakakis