tags:

views:

436

answers:

4
+4  Q: 

jQuery if clicked?

Hi,

With the following piece of code... Is it possible to say, if the li was clicked do this if not(user click somewhere else) do that? The code will only make the div disappear if the user clicks something in the list. I need it to disappear if they click anywhere else as well... ?? Thanks!

.bind('blur', function() {
   $('#search_suggestions_list li').click(function() {
       var selectedString = $(this).text();
       $('#search-box').val(selectedString);
       setTimeout("$('#search_suggestions').fadeOut('fast');", 200);
   })
})
+3  A: 
$(document).click(function(){
   //user clicked anywhere on the page
});

$('#search_suggestions_list li').click(function() {
   //user clicked on the li
   return false;
});

Make sure to return false, otherwise the document click event will be called as well.

munch
$(document).click() will ALWAYS fire on any click
hunter
By returning false, you halt bubbling, so the document.click function will not be run. I may have misunderstood the question, but sounds to me like what he's asking for.
munch
+1 this should work if you add `$('#search_suggestions_list').hide()` or something similar in the document click event, and/or clear the timeout that is showing the search suggestions.
Jim Schubert
Actually, you need to `stopPropagation` instead of returning false.
Josh Stodola
@Josh: `return false` is the same thing as calling both `preventDefault` AND `stopPropogation`
munch
Here's a Nabble discussion about what return false does - http://old.nabble.com/What-does-%27return-false%27-do--td19577249s27240.html
munch
@munch Exactly, and it calls both of those because that is how jQuery is programmed to handle `return false`. In the question, the asker does not specify that he wants to prevent the default action of the link. In fact, in his code sample he is not preventing the default action, so why would I assume he wants to do that now? I never assume anything. I do not want to start an argument here, I just like to be as precise as possible when I answer a question :)
Josh Stodola
@josh: might I ask what the default action of clicking an li is? ;). I was trying to cover the bases, but it is true, your answer is more precise.
munch
Just wanted to say thanks... And, not returning FALSE worked just fine!
mike
A: 
$('#search_suggestions_list li').click(function() {
   var selectedString = $(this).text();
   $('#search-box').val(selectedString);
   setTimeout("$('#search_suggestions').fadeOut('fast');", 200);
})

$('#search-box').blur(function(){
    // do something else
});
hunter
A: 

Mike, I've written something similar to this for a Google Chrome Extension using plain JavaScript (it'd be easy enough to adapt to jQuery).
You can check out that code here: http://code.google.com/p/select-actions/source/browse/select_actions.js

To convert this to meet your requirements, you could try this:

  • in document ready, add a listener that watches for mouse up
  • get the element from which the mouseup occurred
  • conditionally handle your clicks

Feel free to use/modify my code to meet your needs.

Edit: If you'd like to test the functionality to see if it meets your needs, the Chrome Extension is in the Gallery: https://chrome.google.com/extensions/detail/kgjnfpggcjmlkfbicpiedhkcbbfeoohl?hl=en-US

Jim Schubert
+1  A: 

Handle the click event on the document, and then handle the click event on your li. Inside the click event for the li, use the stopPropogation function to prevent the document (parent) click event from firing...

$(document).click(function() {
  // Clicked outside the LI
});

$('#search_suggestions_list li').click(function(e) {
  var selectedString = $(this).text(); 
  $('#search-box').val(selectedString); 
  setTimeout("$('#search_suggestions').fadeOut('fast');", 200);

  e.stopPropagation(); // Stops document.click from firing
}) 
Josh Stodola