title: CSS active pseudo-class changing state swallows up onclick event
body: <p>I'm adapting an example found <a href="http://bytutorial.com/blogs/css/css-vertical-fly-out-menu#" rel="nofollow">here</a> to be a little more like a "real" menu in that it should collapse on user clicks.</p>
  
  <p>I rather like the fact that all of the visual aspects of the menu are dealt with in css, with javascript coming into play only to handle the action required when a user selects an option. i feel this is the most appropriate approach for my particular application.</p>
  
  <p>However, I'm finding the onclick event I'm assigning to each link is being deleted (or ignored) when the menu is put away. </p>
  
  <p>I'm posting 2 versions, one that works in the respect that when users select an option, the desired onclick event is triggered, and another that nicely puts away the menu, but also swallows up the onclick event. I'm after a solution that will combine the 2 results into a working menu, which also satisfies the requirement of putting itself away after a click.</p>
  
  <p><strong>EDIT</strong> just noticed some extra code I have commented out (an earlier attempt before figuring out css only solution. i was calling mouseleave, with no effect.</p>
  
  <p><strong><em>The only difference between the 2 versions is the addition of a CSS clause:</em></strong></p>
  
  <pre><code>#menuwrapper ul:active{
      display:none;
  }
  </code></pre>
  
  <p><strong>Version that "works", but doesn't collapse the menu</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>   var display = document.getElementById("display");
     $(document).ready(function() {
       $('li.menu_clickable').click(function() {
         if (this.children.length === 1) {
           //$(this).trigger('mouseleave');
           display.innerHTML = 'you clicked ' + this.innerHTML;
         }
       });
     });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>/* Define the body style */
  
  body {
    font-family: Arial;
    font-size: 12px;
  }
  #maindiv {
    position: absolute;
    left: 200px;
    top: 0px;
  }
  /* We remove the margin, padding, and list style of UL and LI components */
  
  #menuwrapper ul,
  #menuwrapper ul li {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  /* We apply background color and border bottom white and width to 150px */
  
  #menuwrapper ul li {
    background-color: #7f95db;
    border-bottom: solid 1px white;
    width: 150px;
    cursor: pointer;
  }
  /* We apply the background hover color when user hover the mouse over of the li component */
  
  #menuwrapper ul li:hover {
    background-color: #6679e9;
    position: relative;
    z-index: 100;
  }
  /* We apply the link style */
  
  #menuwrapper ul li a {
    padding: 5px 15px;
    color: #ffffff;
    display: inline-block;
    text-decoration: none;
  }
  /**** SECOND LEVEL MENU ****/
  
  /* We make the position to absolute for flyout menu and hidden the ul until the user hover the parent li item */
  
  #menuwrapper ul li ul {
    position: absolute;
    display: none;
  }
  /* When user has hovered the li item, we show the ul list by applying display:block, note: 150px is the individual menu width.  */
  
  #menuwrapper ul li:hover ul {
    left: 150px;
    top: 0px;
    display: block;
  }
  /* we apply different background color to 2nd level menu items*/
  
  #menuwrapper ul li ul li {
    background-color: #cae25a;
  }
  /* We change the background color for the level 2 submenu when hovering the menu */
  
  #menuwrapper ul li:hover ul li:hover {
    background-color: #b1b536;
  }
  /* We style the color of level 2 links */
  
  #menuwrapper ul li ul li a {
    color: #454444;
    display: inline-block;
    width: 120px;
  }
  /**** THIRD LEVEL MENU ****/
  
  /* We need to hide the 3rd menu, when hovering the first level menu */
  
  #menuwrapper ul li:hover ul li ul {
    position: absolute;
    display: none;
  }
  /* We show the third level menu only when they hover the second level menu parent */
  
  #menuwrapper ul li:hover ul li:hover ul {
    display: block;
    left: 150px;
    top: 0;
  }
  /* We change the background color for the level 3 submenu*/
  
  #menuwrapper ul li:hover ul li:hover ul li {
    background: #86d3fa;
  }
  /* We change the background color for the level 3 submenu when hovering the menu */
  
  #menuwrapper ul li:hover ul li:hover ul li:hover {
    background: #358ebc;
  }
  /* We change the level 3 link color */
  
  #menuwrapper ul li:hover ul li:hover ul li a {
    color: #ffffff;
  }
  /* Hide the menu when a link is clicked on */
  
  /*
  #menuwrapper ul:active{
      display:none;
  }
  */
  
  /* Clear float */
  
  .clear {
    clear: both;
  }</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="menuwrapper"&gt;
    &lt;ul&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;
      &lt;/li&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Products&lt;/a&gt;
        &lt;ul&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 1&lt;/a&gt;
            &lt;ul&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 1&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 2&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 3&lt;/a&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 2&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 3&lt;/a&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;About Us&lt;/a&gt;
        &lt;ul&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Faqs&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Contact Us&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Where are we?&lt;/a&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Help&lt;/a&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  
  &lt;div id="maindiv"&gt;
    &lt;div&gt;
      &lt;label id="display"&gt;click an option&lt;/label&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;button onclick="alert('hey!');"&gt;
        some button
      &lt;/button&gt;under the menu
    &lt;/div&gt;
    &lt;div&gt;
      &lt;input value="some input box"&gt;under the menu
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>version that collapses the menu, but doesn't trigger the onclick</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>   var display = document.getElementById("display");
     $(document).ready(function() {
       $('li.menu_clickable').click(function() {
         if (this.children.length === 1) {
           //$(this).trigger('mouseleave');
           display.innerHTML = 'you clicked ' + this.innerHTML;
         }
       });
     });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>/* Define the body style */
  
  body {
    font-family: Arial;
    font-size: 12px;
  }
  #maindiv {
    position: absolute;
    left: 200px;
    top: 0px;
  }
  /* We remove the margin, padding, and list style of UL and LI components */
  
  #menuwrapper ul,
  #menuwrapper ul li {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  /* We apply background color and border bottom white and width to 150px */
  
  #menuwrapper ul li {
    background-color: #7f95db;
    border-bottom: solid 1px white;
    width: 150px;
    cursor: pointer;
  }
  /* We apply the background hover color when user hover the mouse over of the li component */
  
  #menuwrapper ul li:hover {
    background-color: #6679e9;
    position: relative;
    z-index: 100;
  }
  /* We apply the link style */
  
  #menuwrapper ul li a {
    padding: 5px 15px;
    color: #ffffff;
    display: inline-block;
    text-decoration: none;
  }
  /**** SECOND LEVEL MENU ****/
  
  /* We make the position to absolute for flyout menu and hidden the ul until the user hover the parent li item */
  
  #menuwrapper ul li ul {
    position: absolute;
    display: none;
  }
  /* When user has hovered the li item, we show the ul list by applying display:block, note: 150px is the individual menu width.  */
  
  #menuwrapper ul li:hover ul {
    left: 150px;
    top: 0px;
    display: block;
  }
  /* we apply different background color to 2nd level menu items*/
  
  #menuwrapper ul li ul li {
    background-color: #cae25a;
  }
  /* We change the background color for the level 2 submenu when hovering the menu */
  
  #menuwrapper ul li:hover ul li:hover {
    background-color: #b1b536;
  }
  /* We style the color of level 2 links */
  
  #menuwrapper ul li ul li a {
    color: #454444;
    display: inline-block;
    width: 120px;
  }
  /**** THIRD LEVEL MENU ****/
  
  /* We need to hide the 3rd menu, when hovering the first level menu */
  
  #menuwrapper ul li:hover ul li ul {
    position: absolute;
    display: none;
  }
  /* We show the third level menu only when they hover the second level menu parent */
  
  #menuwrapper ul li:hover ul li:hover ul {
    display: block;
    left: 150px;
    top: 0;
  }
  /* We change the background color for the level 3 submenu*/
  
  #menuwrapper ul li:hover ul li:hover ul li {
    background: #86d3fa;
  }
  /* We change the background color for the level 3 submenu when hovering the menu */
  
  #menuwrapper ul li:hover ul li:hover ul li:hover {
    background: #358ebc;
  }
  /* We change the level 3 link color */
  
  #menuwrapper ul li:hover ul li:hover ul li a {
    color: #ffffff;
  }
  /* Hide the menu when a link is clicked on */
  
  
  #menuwrapper ul:active{
      display:none;
  }
  
  
  /* Clear float */
  
  .clear {
    clear: both;
  }</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="menuwrapper"&gt;
    &lt;ul&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;
      &lt;/li&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Products&lt;/a&gt;
        &lt;ul&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 1&lt;/a&gt;
            &lt;ul&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 1&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 2&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Sub Product 3&lt;/a&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 2&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Product 3&lt;/a&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;About Us&lt;/a&gt;
        &lt;ul&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Faqs&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Contact Us&lt;/a&gt;
          &lt;/li&gt;
          &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Where are we?&lt;/a&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li class="menu_clickable"&gt;&lt;a href="#"&gt;Help&lt;/a&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  
  &lt;div id="maindiv"&gt;
    &lt;div&gt;
      &lt;label id="display"&gt;click an option&lt;/label&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;button onclick="alert('hey!');"&gt;
        some button
      &lt;/button&gt;under the menu
    &lt;/div&gt;
    &lt;div&gt;
      &lt;input value="some input box"&gt;under the menu
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>