GifCo
GifCo

Reputation: 1373

Trouble with jquery .hover function

Having an issue with the jquery .hover function. I am only able to get it to work if I wrap it in a generic $(function(){ I have seen it done without needing this generic function, if anyone can see what i am doing wrong I would appreciate it.

This does not Work:

$('#slider > img').hover(function () {
    stopLoop();
}, function () {
    startSlider();
});

This does work:

$(function () {
    $('#slider > img').hover(function () {
        stopLoop();
    }, function () {
        startSlider();
    });
});

Upvotes: 0

Views: 65

Answers (1)

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

Simple answer is You are trying to bind the events to tags while they are not exists in the dom. So Make it in $(document).ready() or $(function()

$(document).ready(function() {
    $('#slider > img').hover(function () {
        stopLoop();
    }, function () {
        startSlider();
    });
});

Upvotes: 2

Related Questions