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
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts

    JQuery check if objects are hidden

    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:

    Code:
    $(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!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Welcome to the world of asynchronous methods

    The hide("slow") will slowly hide the image while(!) the following if statement is already executed and at that point of time the image is not yet :hidden

    Asynchronous methods usually provide a means to specify a so called "callback" which is a function that will be called as soon as the asynchronous method finished executing.

    Code:
    $("#image, #name").hide("slow", function() {
       // now #image and #name are hidden!
       alert('ok');
    });

  • Users who have thanked devnull69 for this post:

    angelali (10-31-2012)

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I did this, I want it like this, but the alert box is not appearing..

    http://jsfiddle.net/angelali2013/TAmNX/1/

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Code:
    if ($('#image1:visible, #image2:visible, #image3:visible, #image4:visible').length == 0) {

  • Users who have thanked devnull69 for this post:

    angelali (10-31-2012)

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thank you for responding, but it is executing two alert boxes after hiding. I want that when all images are hidden, then it executes the alert box, and of course only one time..

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    342
    Thanks
    38
    Thanked 0 Times in 0 Posts
    It seems it is working now, I change 'is' to filter. Thank you. if i get issues, i will come back later.


  •  

    Posting Permissions

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