0

I have mouseover and mouseout events that I want to be binded all the time. i.e. if I mouseover an element, then mouseout, then mouseover, and so on, these event handlers should still trigger. How should I fix this? Right now they are only binding once....

    $welcome = $("#welcome");
    $welcomeClone = $("#welcome").clone();

    $welcome.mouseover(function() {
        $welcome.css("height","400px");
    });

    $welcome.mouseout(function() {
        $welcome.hide();
        $("#vertical-center").append($welcomeClone)
    });
3
  • You mean you're newly appended clone of $welcome doesnt have any events.. Commented Jun 6, 2014 at 21:29
  • Ohhhhhh that makes sense. How should I fix that? Commented Jun 6, 2014 at 21:31
  • have a look at .bind() or .on() function's Commented Jun 6, 2014 at 21:31

1 Answer 1

1

The reason is because each mouseout you hide the $welcome element - then append a clone of it to #vertical-center - that cloned element doesnt have the same event handlers. You should use event delegation:

$welcome = $("#welcome");
$welcomeClone = $("#welcome").clone();

$("#vertical-center").on("mouseover", "#welcome", function() {
    $(this).css("height", "400px");
}).on("mouseout", function() {
    $(this).hide();
    $("#vertical-center").append($welcomeClone)
});
Sign up to request clarification or add additional context in comments.

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.