1

It sounds simple, and there's a solution involving altering CSS classes, but I was wondering if there's an elegant, on the fly solution to the following issue.

This is a similar question, but it creates "too much recursion" for my scenario.

So I have 2 buttons that both go to the next tab; one at the top of the content, one below. And I want them both to show the state of hover when I hover over either one of them. They have the same html code;

<a href="#" class="button step-next" onclick="showStep(3); return false;">Next &raquo;</a>

and following the other post, I've tried the solution;

$('a.step-next').on('mousenter mouseleave', function(e) {
    $('a.step-next').not(this).trigger(e.type);
});

But my initial logic of using the not() is wrong; it'll just fire the event back and forward between each of the 2 elements.

Clearly, I can alter the CSS to add a new class and remove it on hover/not, which is the route I'll go down. But is there a trigger-only solution?

Community
  • 1
  • 1
Jez
  • 111
  • 7

2 Answers2

0

Not sure what you want to achieve(html required) but this might be the solution you are looking for :

$('a.step-next').on('mousenter mouseleave', function(e) {
  $(this).siblings('a.step-next').trigger(e.type);
});
Jai
  • 74,255
  • 12
  • 74
  • 103
  • In this instance the 2 elements don't sit within the same parent, otherwise this could of been a solution. Thanks for the reply. – Jez Mar 11 '13 at 11:34
0

If i understand correctly you do not want the code-generated event to re-trigger the code..

If so, you can use the extraParameters of .trigger() method

$('a.step-next').on('mousenter mouseleave', function(e, fromCode) {
    if (fromCode === undefined){
        $('a.step-next').not(this).trigger(e.type, true);
    }
});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Although this doesn't resolve my need to use CSS classes to keep the hover style, See the [link here](http://forum.jquery.com/topic/jquery-triggering-css-pseudo-selectors-like-hover) - this does actually resolve the specific question of preventing the infinite loop that my code was generating, so I'm happy with the answer. – Jez Mar 11 '13 at 11:43