tags:

views:

43

answers:

3

Specifically I'm looking to insert a mouse out event after a click. So suppose you have the following:

<div id="container">
 <a href="#" id="link">Link</a>
</div>

The behavior I'd want is after every "a#link" click, I want to insert a mouse out event on the "id#container" div -- basically only fire the mouse out event if there was a click. Is there a way to bind the two events and setup some kind of if checking variable?

+2  A: 
$('#link').click(function(){
     $('#container').mouseout();
});
a.feng
that would permanently bind the mouseout, so that future mouseouts would fire even if there hadn't been another click. You would need to have the mouseout handler unbind itself, or use `.one()` as g.d.d.c does above.
jasongetsdown
Actually, this would trigger a mouseout that doesn't yet exist. No worries, since this was one of the interpretations my mind jumped to when I first read the question.
TNi
A: 

Try something like:

$('#link').click(function () { $(this).parent().mouseout(function() { } });

wmorrell
+3  A: 

This should get close to what you describe:

$(function () {
  $('#container a').click(function () {
    $(this).parent().one('mouseleave', function () {
      // Whatever you want to happen on mouse out here.
    });
  });
});

That way the event only fires one time as the mouse leaves, but only if they clicked the link.

g.d.d.c
I'm not sure why I assumed it wouldn't work this way but if it does, I'll give you the check.
Gee
Hmm.. doesn't seem to work, the mouseout event on the parent div is fired immediately on without actually leaving the div. Weird. Tested with a alert('debug');
Gee
So you click and it immediately triggers the mouseout? You might try mouseleave instead. I'll adjust the answer to reflect. This is how I normally bind events within other events, so the structure should be valid. I don't know that I can explain the instant firing.
g.d.d.c
Never mind, switched out mouseout for mouseleave and it's working now. Thanks for waking me up! Extended browsing instead of working makes you a zombie!
Gee
Opps, just missed your last comment but I got it anyway.
Gee
@Gee - Glad I could help. :)
g.d.d.c