Daniel Ali
Daniel Ali

Reputation: 23

Simplify mouseover/mouseout jQuery events? (a single event handler?)

the jquery is the best way to make beautyful effects

but I want to know if is't possible to reducing this code

$(function(){
    $("obj").mouseover(function(){
        // do something
    });
    $("obj").mouseout(function(){
        // ...
    });
});

to some thing like

$(function(){
    $("obj").mouseover(function(){
        // do something
    }else{
        // ...
    }
});

Upvotes: 2

Views: 1480

Answers (2)

Jivings
Jivings

Reputation: 23250

Most jQuery functions return a reference to the Object they are attached to so you can actually do:

$("obj").mouseover(function(){
        ...
}).mouseout(function(){
        ...
});

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

You could subscribe to the .hover event which could take 2 callbacks:

$(function() {
    $("obj").hover(function() {
        // do something
    }, function() {
        // ...
    });
});

Upvotes: 1

Related Questions