I have a text input, and a hidden list. The list will show and hide upon focusing and blurring the input. However, I want the list remain showing when I click on the list (outside the input), i.e., only hide the list when click on outside of it. How to achieve that?
==== html ====
<input type="text" id="regioninput">
<ol class="continentlist">
//php code to generate list items
</ol>
==== js ====
$('#regioninput').bind('focus', function() {
$('.continentlist').show();
});
$('#regioninput').bind('blur', function() {
$('.continentlist').hide();
});
$('.continentlist li').live('click', function() {
alert($(this).html());
...
});
You can change your code a bit so instead of using
blurandfocusyou can just useclickon different elements:Here is a demo: http://jsfiddle.net/qUeXS/
event.stopPropagation()stops the event from bubbling up the DOM: http://api.jquery.com/event.stoppropagationOn a side-note, this works because of event bubbling. No matter where you click on a document, the
documentobject will receive that click event after it bubbles all the way up the DOM. So if we cancel the bubbling of the event we can stop thedocumentobject from receiving the event.