Jump to content

Jquery Check If Objects Are Hidden


bunnyali2013

Recommended Posts

Hello, I have an issue. I have 10 images in a div. The images' names are in a second div. So, there are 10 images and 10 names.

 

When a user will click on an image, it will hide as well as its respective name. So, if the user clicks on the second image, it will hide as well as its name until everything is hidden.

 

Once the user has clicked all images, an alert box will appear. Refer to the code now for a single image:

 

$(document).ready (function() {
$("#image").click(function() {
$("#image, #name").hide("slow");
if ($("#image").is(":hidden")) {
alert("Ok");
};
});
});

 

As you see, when the user will click an image, it will hide as well as well the name.

This goes the same for all images. The alert box will be displayed once the image is hidden else not,

so I have to verify if the image is hidden. I could give them a single class instead of an ID,

but the user can click only on the image div. Bear in mind, the above example is just for one image only.

I have 10 images, but decided to include example for one in general to make you understand.

The image and the name are hiding, but the alert box is not displayed!

Edited by bunnyali2013
Link to comment
Share on other sites

.is() returns true or false based on whether the elements match the given selector.  If any of the images you selected are visible it will return true, if they are all hidden it will return false.  That false is what you need to check for, not a non-existant .length property.

 

  if (!$('#image1, #image2, #image3, #image4').is(':visible')) {
    alert('I am a bunny');
   }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.