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>
3
  • Do you mean you want to run the same event handler on click and mouseover? Commented Aug 19, 2013 at 14:40
  • No problem, see my answer below. Commented Aug 19, 2013 at 14:44
  • Because of this you should use .on() and not .bind() Commented Aug 19, 2013 at 15:00

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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/

Comments

1

Sure!

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

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

Comments

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

Comments

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");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.