title: How to add an active class to navigation in PHP
body: <p>I am trying to set 'active' classes onto the list nav elements on click. However, the <code>add/removeclass</code> runs before the new page is loaded, thereby setting the <code>active</code> class back to the <code>index.php</code> list element. Isn't the <code>document.ready</code> function supposed to run after the document has finished loading?</p>
  
  <p>Clarifications: All the php files using the same "navbar" elements. The php files are in the same directory level.</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>$(document).ready(function() {  
  
      $(".nav &gt; li").click(function() {  
          $(".nav li").removeClass("active");
          $(this).addClass("active");
      });
      
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;nav class="navbar navbar-inverse navbar-static-top"&gt;
  &lt;div class="container"&gt;
    &lt;div class="navbar-header"&gt;
      &lt;button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"&gt;
        &lt;span class="sr-only"&gt;Toggle navigation&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
        &lt;span class="icon-bar"&gt;&lt;/span&gt;
      &lt;/button&gt;
      &lt;a class="navbar-brand" href="#"&gt;Project name&lt;/a&gt;
    &lt;/div&gt;
    &lt;div id="navbar" class="navbar-collapse collapse"&gt;
      &lt;ul class="nav navbar-nav navbar-right"&gt;
        &lt;li class="active"&gt;&lt;a href="#"&gt;1&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;2&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;3&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;&lt;!--/.nav-collapse --&gt;
  &lt;/div&gt;
  &lt;/nav&gt;</code></pre>
  </div>
  </div>
  </p>