2

I have a web page with elements that when mouse hover over element, text appears inside the element. I want to generate the "Mouse hover"/"mouseenter" to show the text even without the mouse actually hover over the element. Code for example:

HTML

<div> 
    <span id="hover_to_show_text">Hover here to show hidden text</span>
    <span id="hidden_text" style="display:none">Hidden text</span>
</div>    

JS

$( "#hover_to_show_text" ).mouseenter(function() {
   $( #hidden_text ).show();
});
 $( "#hover_to_show_text" ).mouseleave(function() {
   $( #hidden_text ).hide();
});

I want to generate the "mouseenter" that's triggers the "$( "#hover_to_show_text" ).mouseenter(function() {" in jQuery, and leave to "mouseenter" there for a N seconds.

I tried (separately):

$("#hover_to_show_text").hover();
$("#hover_to_show_text").trigger('mouseover');
$("#hover_to_show_text").trigger('mouseenter');

Didn't work. Is there a way to do it?

Thanks.

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
BenB
  • 2,747
  • 3
  • 29
  • 54
  • Not sure I fully understand your question. But after putting your code in jsfiddle it seems to trigger mouseover and mouseenter successfully http://jsfiddle.net/p694huuk/ – EmileKumfa Jul 20 '15 at 23:50
  • I dont think you are doing it right your code runs fine....are you sure you have jquery in your project – code Jul 20 '15 at 23:51
  • You have typos in your code above. And `$("#hover_to_show_text").trigger("mouseenter");` should work. – epascarello Jul 20 '15 at 23:53
  • @epascarello It dosnt work. For example here in stack overflow when you hover over "users" in menu it will be orange background. But if you will run "$("#nav-users").trigger("mouseenter");" nothing will happen. – BenB Jul 21 '15 at 00:04
  • Because "users" uses CSS psudeo :hover which is not JavaScript event. You are not going to be triggering that. – epascarello Jul 21 '15 at 00:10
  • @epascarello I see. Is there a way to from jquery to triiger event the CSS psudeo :hover to run without actuall real hover? – BenB Jul 21 '15 at 00:21
  • @epascarello I see its not possible. http://stackoverflow.com/questions/17226676/how-do-i-simulate-a-mouseover-in-pure-javascript-that-activates-the-css-hover Thanks. – BenB Jul 21 '15 at 00:26

1 Answers1

4

Should work. The events are triggered almost immediately. So you might not have seen the difference.

Encase the tiggering of mouseleave inside a setTimeout to see the difference.

$( "#hover_to_show_text" ).mouseenter(function() {
   $('#hidden_text').show();
});
 $( "#hover_to_show_text" ).mouseleave(function() {
   $('#hidden_text').hide();
});

$("#hover_to_show_text").trigger('mouseover');

setTimeout(function() {
    $("#hover_to_show_text").trigger('mouseleave');
}, 1500);

Check Fiddle

Update

// Just triggering the click event will not work if no
// click handler is provided.
// So create one first
$("#btn").on('click', function () {

    // trigger the mouse enter event 
    $("#hover_to_show_text").trigger('mouseenter');

    setTimeout(function () {
        $("#hover_to_show_text").trigger('mouseleave');
    }, 1500);

});

Update Fiddle

Sushanth --
  • 55,259
  • 9
  • 66
  • 105