Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sorry if my title isn't very clear. I wasn't sure what I should post this as. So here is the issue...

I have this jQuery Code.

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

What I am trying to do is 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. Thanks for your help.

-Kris

share|improve this question
What is the HTML for .remove and .tooltip, how are they related in the DOM? – John Koerner Jul 12 '12 at 14:25
Thanks everybody for your input and quick responses. – Kris Hollenbeck Jul 12 '12 at 14:38

migrated from stackoverflow.com Jul 12 '12 at 16:05

4 Answers

up vote 2 down vote accepted

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(); }
    });
});
share|improve this answer
This works for me. Thanks! – Kris Hollenbeck Jul 12 '12 at 14:33
I was assuming I would need some sort of loop, but wasn't sure exactly how to go about with it. I will accept your answer as soon as the system allows me to. Thanks again. – Kris Hollenbeck Jul 12 '12 at 14:35
@KrisHollenbeck: No problem! Some more details about the ever-useful each can be found here: api.jquery.com/each – rynah Jul 12 '12 at 14:36
Isn't an each loop unnecessary? Why just attach to all .remove elements at once? Like this. – iambriansreed Jul 12 '12 at 14:40
@anonymousdownvotingislame: Please see all my other comments about why index won't work. – rynah Jul 12 '12 at 15:18
show 3 more comments

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.

share|improve this answer

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!

share|improve this answer

Simple enough.

$('.remove').hover(function(){
    $('.tooltip').eq($(this).index()).show();
}, function(){
    $('.tooltip').eq($(this).index()).hide();
});
share|improve this answer
index() will find the position of an element relative to its siblings, so this will only work if all .remove elements are side-by-side in one element with nothing else in it - not really a likely scenario. – rynah Jul 12 '12 at 16:00
@minitech The OP uses the index of the .remove element in determining which .tooltip element to select which is no different that what I am doing above. – iambriansreed Jul 12 '12 at 16:02
Nope, that's .eq - completely different. It's the index of the element relative to the entire collection. – rynah Jul 12 '12 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.