Custom Script not called
Describe the bug The jquery just doesn't fire up correctly when I try to change the class on nav-link. Maybe this has been blocked by other js?
I am creating my index.php file which includes once header.php, sidebard.php, and footer.php Some page is called up with condition selected from the sidebar menu. I try to change the active nav-link when another item is clicked and add "active" to the class. But it just doesn't fire up.
Here is the content of my sidebar.php
<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-light-primary">
<!-- Sidebar -->
<div class="sidebar">
<!-- Sidebar Menu -->
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<li class="nav-item">
<a href="?page=home" class="nav-link">
<i class="nav-icon fas fa-home"></i>
<p>
Home
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=notification" class="nav-link" id="nav-link">
<i class="nav-icon fas fa-info-square"></i>
<p>
Notification
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=important" class="nav-link">
<i class="nav-icon fas fa-calendar-alt"></i>
<p>
Important Dates
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=eligibility" class="nav-link">
<i class="nav-icon fas fa-badge-check"></i>
<p>
Eligibility
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=feestructure" class="nav-link">
<i class="nav-icon fas fa-money-check-alt"></i>
<p>
Fee Structure
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=prospectus" class="nav-link">
<i class="nav-icon fas fa-file-powerpoint"></i>
<p>
Prospectus
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=apply" class="nav-link">
<i class="nav-icon fas fa-user-plus"></i>
<p>
Apply Now
</p>
</a>
</li>
<li class="nav-item">
<a href="?page=notification" class="nav-link">
<i class="nav-icon fas fa-user-lock"></i>
<p>
Applicant Login
</p>
</a>
</li>
<li class="nav-item">
<a href="/admin/" class="nav-link">
<i class="nav-icon fas fa-user-shield"></i>
<p>
Admin Login
</p>
</a>
</li>
</ul>
</nav>
<nav class="mt-5">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<li class="nav-item has-treeview menu-open">
<a href="" class="nav-link active">
<i class="nav-icon fas fa-question-circle"></i>
<p>
ADMISSION HELPLINE
<i class="right fas fa-angle-left"></i>
</p>
</a>
<ul class="nav nav-treeview">
<li class="nav-item">
<a class="nav-link active">
<i class="far fa-empty"></i>
<p><b>Principal Office</b><br>
<b>Contact:</b> (9:30AM – 4:00PM) <br>+1 1234 567 890<br> <b>Email:</b> <br>[email protected]</p>
</a>
</li>
</ul>
</li>
</ul>
</nav>
<!-- /.sidebar-menu -->
</div>
<!-- /.sidebar -->
</aside>
And here is my footer.php which include the jquery to fire the action.
<!-- Main Footer -->
<footer class="main-footer">
<!-- Default to the left -->
<strong>The Best Dashboard theme ever made aka AdminLTE</strong>;
</footer>
</div>
<!-- ./wrapper -->
<!-- REQUIRED SCRIPTS -->
<!-- script to change the selected active nav-link -->
<script>
$(document).ready(function () {
$('.nav-link').click(function(e) {
$('.nav-link').removeClass('active');
$(this).addClass("active");
});
});
</script>
<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
</body>
</html>
Environment
- AdminLTE Version: [e.g. v3.0.4]
- Operating System: [e.g. Microsoft Windows 10 ]
- Browser (Version): [e.g. Chrome (Latest)]
Am I missing something? Any help me please.
I would guess your recieve console errors, like $/jquery is not definied.
Try to replace your footer.php with this 😄
<!-- Main Footer -->
<footer class="main-footer">
<!-- Default to the left -->
<strong>The Best Dashboard theme ever made aka AdminLTE</strong>;
</footer>
</div>
<!-- ./wrapper -->
<!-- REQUIRED SCRIPTS -->
<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- script to change the selected active nav-link -->
<script>
$(document).ready(function () {
$('.nav-link').click(function(e) {
$('.nav-link.active').removeClass('active');
$(this).addClass("active");
});
});
</script>
</body>
</html>
Thank you very much for your prompt reply.
The console shows no error at all so far with the firefox browser.
I replace the footer.php with your code,bingo!...it works.
When I click the sidebar menu item, it fires up the function. But went back to the predefined active class. I will try to figure out where I might had the code.

My index.php looks like this...
`<?php include_once 'header.php'; include_once 'sidebar.php';
if($_GET['page'] == "home"){
require_once 'home.php';
}
.....
.....
else{
require_once 'home.php';
}
include_once 'footer.php';
?>`
It look like you go to annew page with a page load instead with something like ajax.
Try this script instead of your nav-link click script:
var url = window.location;
// for sidebar menu entirely but not cover treeview
$('ul.sidebar-menu a').filter(function() {
return this.href != url;
}).parent().removeClass('active');
// for sidebar menu entirely but not cover treeview
$('ul.sidebar-menu a').filter(function() {
return this.href == url;
}).parent().addClass('active');
// for treeview
$('ul.treeview-menu a').filter(function() {
return this.href == url;
}).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active menu-open');
This script comes from this issue https://github.com/ColorlibHQ/AdminLTE/issues/1482#issuecomment-559347681.
Now I could make it works with your help. Thanks alot. REJack. :-)
Cheer!