1

I have a list of items, each of which needs to show a div. Click on one, and it shows you a div with tweets in it. I've been able to successfully show the divs with jQuery. Unfortunately, I have no clue how to hide the divs when the user clicks outside of said div. I've tried fiddling around with the .not() selector, but I just can't do it. Here's my code:

$("#twitter-wid").click(function() {
$(".recent_tweets").fadeIn(400);
});

In this case, #twitter-wid is the 'li' item, and .recent_tweets is the div element that I need to show. I want it such that when the user clicks anywhere out of .recent_tweets that it should hide. I've so far tried to add:

$.not('.recent_tweets').click(function() {
$('.recent_tweets').hide();
});

But to no avail. Oh and I'm new to jQuery. Thanks!

EDIT: I wonder how Twitter does it for their little drop-down menu. Click on your username and it drops down a bunch of options. Click anywhere out and it slides up.

1
  • If you wonder how twitter does it: go read their code. There is absolutely nothing wrong with reading through code someone else wrote to see how they solved a problem. Just don't copy-pasta it unless you're allowed to. Commented Nov 8, 2010 at 5:48

2 Answers 2

2

There is outside event jquery plugin you may want to use:

$(".recent_tweets").bind( "clickoutside", function(event){
  $(this).hide();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The problem is that now I can't get it to show when #twitter-wid is clicked (because that's outside .recent_tweets).
1
 $(document).click(function() { //Click anywhere
  $('.recent_tweets').hide();
 });

//ADDED for inside click

$('.recent_tweets').click(function(e){
     e.stopPropagation();
 });

6 Comments

this would still hide the div if they clicked inside it... perhaps add a check to see if the mouse loc is inside recent_tweets?
Yep, @dpmguise is right. Now the .recent_tweets div doesn't show at all.
you can stop all propagation of the event when you click inside
this link might be useful for you rob-bell.net/2009/02/…
^^ I think that should be useful. I'll look through that code and figure it out. Thanks, you've been of great help!
|

Your Answer

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.