0

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Jitender
  • 7,593
  • 30
  • 104
  • 210

6 Answers6

3

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+.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2
$(function(){
    $('a').on('click mouseover', function() {
       alert(0);
       return false;
    });      
});
Shivam
  • 2,208
  • 3
  • 24
  • 39
1

Sure!

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

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

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

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."

Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
1

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/

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

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");
});
Smern
  • 18,746
  • 21
  • 72
  • 90