3
\$\begingroup\$

I am trying to hide and show tooltips depending on what element is hovered over. This works as expected and I could continue to rinse and repeat. However, I am wondering if there is a good way to simplify this or make it more compact. Also I was assuming I could use .hover(), but that didn't seem to work for me. So instead I am using mouseover and mouseout.

$('.remove').eq(0).mouseover(function(){
 $('.tooltip').eq(0).show();
});
$('.remove').eq(0).mouseout(function(){
 $('.tooltip').eq(0).hide();
});
$('.remove').eq(1).mouseover(function(){
 $('.tooltip').eq(1).show();
});
$('.remove').eq(1).mouseout(function(){
 $('.tooltip').eq(1).hide();
});
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 12, 2012 at 14:23
\$\endgroup\$
1
  • \$\begingroup\$ What is the HTML for .remove and .tooltip, how are they related in the DOM? \$\endgroup\$ Commented Jul 12, 2012 at 14:25

3 Answers 3

6
\$\begingroup\$

Use an .each "loop":

var tooltips = $('.tooltip');
$('.remove').each(function(i) {
 var tooltip = tooltips.eq(i);
 $(this).on({
 mouseover: function() { tooltip.show(); },
 mouseout: function() { tooltip.hide(); }
 });
});
answered Jul 12, 2012 at 14:26
\$\endgroup\$
6
  • \$\begingroup\$ @KrisHollenbeck: No problem! Some more details about the ever-useful each can be found here: api.jquery.com/each \$\endgroup\$ Commented Jul 12, 2012 at 14:36
  • \$\begingroup\$ Isn't an each loop unnecessary? Why just attach to all .remove elements at once? Like this. \$\endgroup\$ Commented Jul 12, 2012 at 14:40
  • \$\begingroup\$ @anonymousdownvotingislame: Please see all my other comments about why index won't work. \$\endgroup\$ Commented Jul 12, 2012 at 15:18
  • \$\begingroup\$ Also, I agree with @anonymousdownvotingislame's username. A comment when you downvote me would be much appreciated, random person. \$\endgroup\$ Commented Jul 12, 2012 at 15:54
  • \$\begingroup\$ I see no comments regarding index()? \$\endgroup\$ Commented Jul 12, 2012 at 15:59
1
\$\begingroup\$

If i'm not totally wrong try referencing your target through a custom attribute on the trigger:

<div id="tooltip1" style="display:none">TOOLTIP 1 CONTENT</div>
<div id="tooltip2" style="display:none">TOOLTIP 2 CONTENT</div>
<div id="tooltip3" style="display:none">TOOLTIP 3 CONTENT</div>
<a href="javascript:;" class="remove" data-ref="#tooltip1">TRIGGER 1</a>
<a href="javascript:;" class="remove" data-ref="#tooltip2">TRIGGER 2</a>
<a href="javascript:;" class="remove" data-ref="#tooltip3">TRIGGER 3</a>
<script type="text/javascript">
 $('.remove').mouseover(function(){
 $($(this).attr("data-ref")).show();
 });
 $('.remove').mouseout(function(){
 $($(this).attr("data-ref")).hide();
 });
</script>

there's room for improvement but should help.

Happy coding!

answered Jul 12, 2012 at 14:34
\$\endgroup\$
0
0
\$\begingroup\$

Have a single mouse move event handler that tracks the element id it's currently on and set the tooltip based on it. For improved performance you could instead poll mouse's position every say 500 ms and set the tooltip based on that.

You would have a single function to manage all your tooltips in a switch(){} block of code with a "default" case that sets the tooltip to empty.

answered Jul 12, 2012 at 14:28
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.