Using jQuery, I am trying to create a dynamic menu bar whose selections change as I click on different items.
Here is the simple HTML
<div class="global">
<nav class='menubar'>
<button class='btn websites'>Websites</button>
<button class='btn contacts'>Contacts</button>
<button class='btn finance'>Finance</button>
</nav>
<div class="workspace">
</div>
</div>
And the javascript
// click on navbar only
$($nav_menu).on('click', (e)=> {
console.log("Menu Bar")
$nav_menu.load('/main_menu.html');
})
// click on any button
$($nav_menu).on('click', $btn_websites, ()=> {
//$nav_menu.html("");
console.log("Websites")
//$nav_menu.load('/website_menu.html');
})
// click on any button
$($nav_menu).on('click', $btn_contacts, ()=> {
//$nav_menu.html("");
console.log("Contacts")
//$nav_menu.load('/contact_menu.html');
})
// click on any button
$($nav_menu).on('click', $btn_finance, ()=>{
//$nav_menu.html("");
console.log("Finance")
// $nav_menu.load('/finance_menu.html');
})
The problem is that regardless of which item I click on, or if I click only on the menu bar itself, all of the functions are executed. Console log shows that each function has fired.
Thank you
$btn_websitesa jquery object or string selector?$btn_websites/$btn_contacts/$btn_financeis, any solution would be just a guess. These must be strings and the outdated$implies these are jquery collections, which don't work for filters on event delegation, so are probably getting ignored, thus your problem. Please see minimal reproducible example.