newbie
newbie

Reputation: 55

jQuery - how to add click and hover in same function

How to mix hover and click in same function?Here I have a hover function,but I want to add click function beside hover function.

$(document).ready(function(){
    $('.img').hover(function (e) {
        e.preventDefault();

        $(". img").css({"opacity":"0.4","filter:":"alpha(opacity=40)"});
        $(this).css({"opacity":"100","filter:":"alpha(opacity=100)"});
        alert('test');
    });
});

Upvotes: 0

Views: 35

Answers (1)

JFK
JFK

Reputation: 41143

You can bind several events using .on() like :

$(".img").on({
    click: function (event) {
        console.log(event)
    },
    mouseenter: function (event) {
        console.log(event)
    },
    mouseleave: function (event) {
        console.log(event)
    }
});

See JSFIDDLE

... or you can also chain different methods like :

$('.img').hover(function (event) {
    console.log(event)
}).on("click", function (event) {
    console.log(event)
});

See JSFIDDLE

Upvotes: 2

Related Questions