Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery: click everywhere but some element
    primarykey
    data
    text
    <p>I have some elements which i can select with .click() function and they became highlighted. There is menu above them with some actions. I want to cancel highlight when I click any element but not menu.</p> <p>Structure:</p> <pre><code>&lt;body&gt; &lt;div id="menu"&gt; &lt;/div&gt; &lt;div id="elements"&gt; /* selectable elements here */ &lt;/div&gt; &lt;/body&gt; </code></pre> <hr> <p><strong>Executable Example</strong></p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$().ready(function(){ $("#elements a").click(function(){ $(this).css('color', 'red'); return false; }); $(document).click(function(e) { if ( $(e.target).closest('#menu').length === 0 ) { $("#elements a").css('color', 'blue'); } }); });</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="menu"&gt; &lt;a href="#"&gt;Menu 1&lt;/a&gt; &lt;a href="#"&gt;Menu 2&lt;/a&gt; &lt;a href="#"&gt;Menu 3&lt;/a&gt; &lt;/div&gt; &lt;div id="elements"&gt; &lt;a href="#"&gt;Element 1&lt;/a&gt; &lt;a href="#"&gt;Element 2&lt;/a&gt; &lt;a href="#"&gt;Element 3&lt;/a&gt; &lt;/div&gt; &lt;div&gt; Click any element to highlight it. Click anywhere to reset highlighting. Click menu to keep highlighting. &lt;/div&gt;</code></pre> </div> </div> </p> <p><strong><a href="http://jsfiddle.net/3aqgLpp6/2" rel="nofollow noreferrer">Jsfiddle</a></strong></p> <p>I tried to bind <code>$("body *").not("#menu").click(...);</code> but when I click on #menu then body's onclick event fired too because #menu is body's children.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload