scorpio1441
scorpio1441

Reputation: 3088

jQuery: how to trigger hover out?

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.

Upvotes: 5

Views: 34904

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195962

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>

Upvotes: 10

Rory McCrossan
Rory McCrossan

Reputation: 337550

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

Upvotes: 9

Related Questions