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.
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);
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.
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.
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).
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>
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With