title: Is there a way to programmatically trigger the TAB key to move the focus to the next focusable element?
body: <p>I am writing a unit test to test the tab order on my webpage and I could not find a way to simulate the user's keying down of the TAB key to focus on the next focusable element, though this seems to be an easy task. </p>
  
  <p>jQuery is allowed while pure javascript solution is preferred. Any kind of help is appreciated.</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>// TODO
  function triggerTab () {
  
  
  }
  
  $("#btn-save").focus();
  triggerTab();
  
  // Expect the focus is on the cancel button now.
  console.log(document.activeElement.id);</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;p&gt; some text &lt;/p&gt; 
  &lt;button id="btn-save"&gt; save &lt;/button&gt;
  &lt;button id="btn-cancel&gt; cancel &lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>P.S. It is no acceptable to call focus() on the next focusable element, even if you can write code to figure out the next focusable element. So this path is NOT what I want:</p>
  
  <pre><code>function getAllFocusables () {
      ...
  }
  
  var focusables = getAllFocusables(),
      index = focusables.indexOf(document.activeElement);
  
  $(focusables[index+1]).focus();
  </code></pre>