Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: how to trigger hover out?

Tags:

jquery

How can I trigger second hover function?

$('#adm1n-toolbar').hover(
    function() {
        ...
    },
    function() {
        ...
    }
);

$('#adm1n-toolbar-content select').change(function(e) {
    ...
    $('#adm1n-toolbar').trigger('mouseout');
});

mouseout or mouseleave does not work.

like image 650
Alex G Avatar asked May 17 '12 13:05

Alex G


People also ask

How can write hover function in jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);

What is Mouseout in jQuery?

jQuery mouseout() MethodThe mouseout event occurs when the mouse pointer leaves the selected element. The mouseout() method triggers the mouseout event, or attaches a function to run when a mouseout event occurs.

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.

What is the difference between Mouseout and Mouseleave?

This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).


2 Answers

It works just fine if you use mouseleave which is what hover uses behind the scenes

demo:

$('#adm1n-toolbar').hover(
  function() {
    console.log('in');
  },
  function(e) {
    $(this).fadeOut('fast').fadeIn('fast');
  }
);

$('#adm1n-toolbar-content select').change(function(e) {
  $('#adm1n-toolbar').trigger('mouseleave');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="adm1n-toolbar">toolbar</div>

<div id="adm1n-toolbar-content">
  <select multiple>
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
    <option>option 4</option>
  </select>
</div>
like image 74
Gabriele Petrioli Avatar answered Oct 09 '22 05:10

Gabriele Petrioli


You can't externally target the second function in hover(). However as hover() is just a shortcut for mouseenter() and mouseleave() you can assign them separately to let you trigger them as you need. Try this:

$('#adm1n-toolbar')
    .mouseenter(function() {
        // ...
    })
    .mouseleave(function() {
        // ...
    });

$('#adm1n-toolbar-content select').change(function(e) {
    $('#adm1n-toolbar').trigger('mouseleave');
});
like image 9
Rory McCrossan Avatar answered Oct 09 '22 06:10

Rory McCrossan