45

Can hover and click functions be combined into one, so for example:

click:

$('#target').click(function() {
  // common operation
});

hover:

$('#target').hover(function () {
    // common operation
});

can they be combined into one function?

Thanks!

0

8 Answers 8

107

Use basic programming composition: create a method and pass the same function to click and hover as a callback.

var hoverOrClick = function () {
    // do something common
}
$('#target').click(hoverOrClick).hover(hoverOrClick);

Second way: use bindon:

$('#target').on('click mouseover', function () {
    // Do something for both
});

jQuery('#target').bind('click mouseover', function () {
    // Do something for both
});
Sign up to request clarification or add additional context in comments.

7 Comments

The jquery "on" function is now recommend over using bind. But it works in the same way, just replace "bind" with "on".
Hint me, please, how I can define what did event (click or hover )happened?
I am getting some problem because of this. I have to call different functions on both hover and click. But when I click on an element both functions are called at a time. Can anyone tell me how can I solve this problem?
@SarfrazAhmad This is because before the user is able to click, she has to hover. Maybe you don't need to do anything on click.
yeah, but how can i prevent a function to be called on hover when i clicked on an element
|
30

Use mouseover instead hover.

$('#target').on('click mouseover', function () {
    // Do something for both
});

Comments

9
$("#target").hover(function(){
  $(this).click();
}).click(function(){
  //common function
});

Comments

9

You can use .bind() or .live() whichever is appropriate, but no need to name the function:

$('#target').bind('click hover', function () {
 // common operation
});

or if you were doing this on lots of element (not much sense for an IE unless the element changes):

$('#target').live('click hover', function () {
 // common operation
});

Note, this will only bind the first hover argument, the mouseover event, it won't hook anything to the mouseleave event.

Comments

1
var hoverAndClick = function() {
    // Your actions here
} ;

$("#target").hover( hoverAndClick ).click( hoverAndClick ) ;

Comments

1

You could also use bind:

$('#myelement').bind('click hover', function yourCommonHandler (e) {
   // Your handler here
});

1 Comment

A heads up for any future readers not already aware 'As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.' api.jquery.com/bind
0

i think best approach is to make a common method and call in hover and click events.

Comments

0
  $("#target").on({
        hover: function(){
           //do on mouse hover
        },  
        click: function(){
            //do on mouse click
        }  
    });

1 Comment

When giving an answer it is preferable to give some explanation as to WHY your answer is the one.

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.