I'm facing a puzzling situation. I created a navbar using jQuery Mobile following the instructions on their official website: jQuery Mobile Navbar Demos
Everything functions smoothly until I introduce href attributes in the list elements within the unordered list (the selected items in the navbar fail to activate):
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div id="menu">
<div data-role="navbar">
<ul>
<li><a href="" class="contentUpdate" id="1"><i class="fas fa-swimmer"></i>Swim</a></li>
<li><a href="" class="contentUpdate" id="2"><i class="fas fa-biking"></i>Cycle</a></li>
<li><a href="" class="contentUpdate" id="3"><i class="fas fa-running"></i>Run</a></li>
</ul>
</div>
<!-- /navbar -->
</div>
When I modify the href attribute to link to another div (for a webpage where clicking the navbar item changes content), the active class fails to trigger when clicked.
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div id="menu">
<div data-role="navbar">
<ul>
<li><a href="#swim" class="contentUpdate" id="1"><i class="fas fa-swimmer"></i>Swim</a></li>
<li><a href="#cycle" class="contentUpdate" id="2"><i class="fas fa-biking"></i>Cycle</a></li>
<li><a href="#run" class="contentUpdate" id="3"><i class="fas fa-running"></i>Run</a></li>
</ul>
</div>
<!-- /navbar -->
</div>
I attempted adding a class to the items with jQuery, but it did not produce the desired outcome. When I implement the following code, only the first item fails to become active upon clicking:
$("#1").click(function() {
$("#1").addClass("ui-btn-active");
});
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div id="menu">
<div data-role="navbar">
<ul>
<li><a href="#swim" class="contentUpdate" id="1"><i class="fas fa-swimmer"></i>Swim</a></li>
<li><a href="#cycle" class="contentUpdate" id="2"><i class="fas fa-biking"></i>Cycle</a></li>
<li><a href="#run" class="contentUpdate" id="3"><i class="fas fa-running"></i>Run</a></li>
</ul>
</div>
<!-- /navbar -->
</div>
However, the scenario changes when I apply the code below as the remaining two items successfully transition to the active state:
$("#1").click(function() {
$("#1").addClass("ui-btn-active");
$("#2").addClass("ui-btn-active");
$("#3").addClass("ui-btn-active");
});
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<div id="menu">
<div data-role="navbar">
<ul>
<li><a href="#swim" class="contentUpdate" id="1"><i class="fas fa-swimmer"></i>Swim</a></li>
<li><a href="#cycle" class="contentUpdate" id="2"><i class="fas fa-biking"></i>Cycle</a></li>
<li><a href="#run" class="contentUpdate" id="3"><i class="fas fa-running"></i>Run</a></li>
</ul>
</div>
<!-- /navbar -->
</div>
I am perplexed by this behavior. If anyone has insights or suggestions, I would greatly appreciate your help. Here's an example of My Navbar with an active class.