How to bind an event to any elements except one?
Hi,
in the language menu shown below, I would like to show the "current_menu" when
it's not visible and clicking on the "current_page" element, and to hide it when
clicking everywhere except in the "current_menu" element.
I tried with the "not" selector:
- $(":not(#current_menu)").bind('mousedown',function(event) {
- $("#current_menu").hide();
- event.stopPropagation();
- });
I expected it to be "bind this event to all elements except "current_menu",
but the event is still triggered when clicking on the menu because
it seems that the parents are selected.
Anybody can help?
thanks
My code
HTML
- <div id="lang" >
- <div id="current_page"></div>
- <div id="current_menu">
- <ul><li id="en"><a href="">en</a></li>
- <li id="fr"><a href="">fr</a></li>
- <li id="es"><a href="">es</a></li>
- </ul>
- </div>
- </div>
jQuery
- $("#current_menu").hide();
- $("#current_page a").click( function() {
- if( $("#current_menu").is(":visible") ) {
- $("#current_menu").hide();
- return false;
- }
- else {
- $("#current_menu").css('visibility','visible');
- $("#current_menu").show();
- return false;
- }
- });
- $("#current_menu li").mousedown(function(event){
- event.preventDefault(); //prevent synchronous loading
- $.post("/i18n/setlang/", { language: $(this).text(), next: "{{request.path}}" }, function(data) {
- top.location.href="{{request.path}}"; //redirection
- });
- $("#current_page").html($(this).text());
- $("#current_menu").hide();
- });