tags:

views:

2876

answers:

3
$(".item").hover(
  function () {
    $(this).slideDown().append(
      $("<div class=\"attending\">Yes/No</div>")
     );
  }, 
  function () {
    $(this).find("div:last").slideUp().remove();
  }
);

I am using the above code to append a div onto the bottom of an element when rolled over. I have a number of .item divs, each one getting it appended to them on rollover, but for some reason I am unable to make the slidedown/up work.

Any ideas?

+1  A: 

Intuitively I'm giong to say that you're claling the slide effect on the wrong element. It's being called on .list rather than div.attending, which is the box I presume you want to appear.

Andrew Noyes
+1  A: 

try

$(".item").hover(
  function () {
    $(this).append(
        $("<div class=\"attending\">Yes/No</div>").hide();
    ).find("div:last").slideDown();
  }, 
  function () {
    $(this).find("div:last").slideUp("normal", function() {
      $(this).remove();
    });
  }
);
Dmitri Farkov
Doesn't slide at all. Just instant appear/disappear
James
The above answer has the correct version (not my answer). My apologies.
Dmitri Farkov
+6  A: 
$(".item").hover(
    function () {
        var answer = $("<div class=\"attending\">Yes/No</div>").hide();
        answer.appendTo($(this)).slideDown();
    }, 
    function () {
        $(this).find("div:last").slideUp("normal", function() {
            $(this).remove();
        });
    }
);

It doesn't slide down!

You appended the wrong child to the wrong element and you forgot to hide the element first.

It doesn't slide up!

This line starts sliding up, but doesn't wait for the animation to finish and removes the element.

$(this).find("div:last").slideUp().remove()
Georg
This one has it. The .remove() needs to be called as part of a callback function in the .slideUp(). Otherwise they run at the same time. The callback ensures the removal won't occur until the animation ends.
Steve Paulo
Is there a way to require the cursor be over the element for more than 2 seconds or so?
James
Yes, through Javascript's setTimeout(). It's not directly implemented in jQuery. I suggest you ask in another question.
Georg