Jitender
Jitender

Reputation: 7971

Bind event in jquery

I have click function on a tag. I want to bind mouseover event on same element. It this possible with .bind method. fiddle

$(function(){
    $('a').click(function(){
        alert(0);
    })
    $('a').bind('mouseover')
})
<a href="#">Jitender</a>

Upvotes: 0

Views: 64

Answers (6)

Shivam
Shivam

Reputation: 2248

$(function(){
    $('a').on('click mouseover', function() {
       alert(0);
       return false;
    });      
});

Upvotes: 2

Smern
Smern

Reputation: 19066

Yes, like this:

Fiddle

$('a').bind('mouseover', function () {
    alert(0);
});

Also, bind() is outdated, if you are using a newer version of jquery (1.7+) you should be using on() instead.

As it's hard to see both mouseover and click events that create an alert (since the alert from the mouseover would prevent you from ever clicking on it), the following will allow you to see both events working better:

Fiddle

$('a').on('mouseover click', function(){
    $(this).toggleClass("test");
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Assuming you want to bind the same handler to the click and mouseover event you can try this:

$('a').on('click mouseover', function(e) {
    e.preventDefault();
    alert('0');
}); 

Note the usage of on is preferred over bind in jQuery 1.7+.

Upvotes: 3

Richard Dalton
Richard Dalton

Reputation: 35793

Yes. Just bind the mouseover following the click binding:

$('a').click(function(){
    alert(0);
}).bind('mouseover', function() {
    $(this).css('background-color', 'red'); // To show it working
});

http://jsfiddle.net/R7qrC/3/

Upvotes: 1

Nathan Koop
Nathan Koop

Reputation: 25197

You should use the on keyword.

$('a').on('mouseover', function() { alert(1);})

Per the jQuery documentation:

"As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document."

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

Sure!

$('a').mouseover(function() {
    alert("Moused!");
});

Demo: http://jsfiddle.net/R7qrC/2/

Upvotes: 1

Related Questions