6

I need to bind the click function on each div of this ordered list in order to make hide/show an image on each imgXX div, I'm newbie with JQuery

<ol id='selectable'>
 <li class="ui-state-default">
   <div id="img01" class="img">
      <div id="star01" class="star">
          <img src="../ima/star.png" height="30px"/>
      </div>
   </div>
 </li>
 <li class="ui-state-default">
   <div id="img02" class="img">
      <div id="star02" class="star">
          <img src="../ima/star.png" height="30px"/>
      </div>
   </div>
 </li>
</ol>

JQuery

$('div').each(function(){
   $(this).click(function(){
          if($(this).find('img').is(':visible').length){
                    $(this).find('img').fadeOut(700);
          }
          else{
                    $(this).find('img').fadeIn(700);
              }
   });
});
3
  • 1
    So what is giving you grief? Umm... ($(this).find('img').is(':visible').length is not correct, I think. is() gives you a true false. Applying length to it might be weird. Commented Sep 8, 2010 at 5:59
  • @Sidhart You are right, it should be find('img:visible') Commented Sep 8, 2010 at 6:06
  • Note that you are binding the click event on the nested div elements, so they might fire twice. You may want to use $('div.img') or $('div.star') to single out one set of div elements. Also, .each(function(){ $(this).click(...); }) can be shortened to just .click(...) as it applies the event to all elements in the collection. Commented Sep 8, 2010 at 6:12

5 Answers 5

7

Try this:

$('div').each(function(){ 
   $(this).click(function(){ 
          if($(this).find('img').is(':visible')){ 
                    $(this).find('img').fadeOut(700); 
          } 
          else{ 
                    $(this).find('img').fadeIn(700); 
              } 
   }); 
}); 
Sign up to request clarification or add additional context in comments.

Comments

5

The is method returns a boolean. Use:

if($(this).find('img').is(':visible'))

or:

if($(this).find('img:visible').length)

Comments

1

Unlike the other filtering and traversal methods, .is() does not create a new jQuery object. Instead, it allows us to test the contents of a jQuery object without modification.

So, you can not use length on it as it returns a boolean value. Remove 'length' and it should work.

Comments

0

I don't think you necessarily need the each.

$('div').click(function(){

  var img = $(this).find('img');

  if($(img).is(':visible')){
    $(img).fadeOut(700);
  }

});

Comments

0

Not sure about the nesting of the divs, but since you are requesting to only fade the img I am assuming that the .star div is visible and clickable. The function below is a bit more efficient as I am using children instead of find which is recursive. Other than the selectors this should work for you.

$('div.star').click(function() {
    function () {
        $(this).children('img').fadeOut(700);
    },
    function () {
        $(this).children('img').fadeIn(700);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.