I am building a Single page Application in JavaScript. I am following this example to use BootStrap 5 menu and dropdown menu.
How can I add code to handle when user select a menu and an item in the drop down menu?
Here is the example of my menu bar:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
When a user selects a dropdown item, a click event is triggered.
Therefore, you can add an event listener to all dropdown-item of dropdown-menu, in order to add your code for handling.
const dropdownItems = document.querySelectorAll(".dropdown-menu .dropdown-item")
dropdownItems.forEach(item => {
item.addEventListener("click", function(e) {
// your code here
})
})
Related
Hi I have an issue with a bootstrap hamburger menu the menu expands, however when I try to collapse the menu it just will not work, the data target class is correct, is there something I am doing wrong here or have I over looked something. Thanks
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-info border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand text-light" asp-area="" asp-controller="Home" asp-action="Index">PobalScoil</a>
<button class="navbar-toggler text-light" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-light bg-info" asp-area="" asp-controller="Home" asp-action="About_Us">About |</a>
</li>
I have a menu that is aligned to the right. The last menu is a dropdown menu. But the dropdown menu items doesn't stay on the screen. Half of it leaves the screen. I have been googling but I can't find the class that kepps it on screen.
<nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container-fluid">
<div class="ms-auto order-0">
<a class="navbar-brand mx-auto" href="#"><img src="./img/BMB-logo.svg" width="75" height="75"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#">Right</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fas fa-user"></i>
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Login</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
As you can see in an older question, the dropdown-menu needs to be right aligned. In Bootstrap 5, dropdown-menu-right has changed to dropdown-menu-end...
<nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container-fluid">
<div class="ms-auto order-0">
<a class="navbar-brand mx-auto" href="#"><img src="//placehold.it/75" width="75" height="75"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#">Right</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fas fa-user"></i>
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Login</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Demo
The accepted answer doesn't work for me due to the following CSS style:
.dropdown-menu[style] {
right: auto !important;
}
which overrides the right:0; property of .dropdown-menu-end. Removing the !important or adding it to the .dropdown-menu-end forces the dropdown to align right as intended.
I have a login page, but whenever I typed wrong credentials it showing the mainheader for login person but with no name.. See the image I attached for further explanations
login
login with wrrong credentials
this is what it look like when the person login successfully, it have name and company
View form
<ul class="nav navbar-nav navbar-right">
#{
AccountContactVM customer = (AccountContactVM)ViewBag.AccountData;
}
#if (customer.User == customer.User && customer.User != null )
{
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<i class="fa fa-user-circle fa-fw fa-lg" aria-hidden="true"</i>
#customer.User.FirstName #customer.User.LastName
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li class="disabled">
<div class="row">
<div class="col-md-3">
<i class="fa fa-user-circle" style="font-size:40px;padding:5px 20px;" aria-hidden="true"></i>
</div>
<div class="col-md-9">
<p style="padding-top:5px;">
#customer.User.FirstName #customer.User.LastName
<br />
#customer.Company.AccountName
</p>
</div>
</div>
</li>
<li class="divider"></li>
<li><a asp-controller="Account" asp-action="Index">Customer Portal</a></li>
<li class="divider"></li>
<li><a a asp-controller="Account" asp-action="LogOut">Logout</a></li>
</ul>
</li>
}
else
{
<li><a asp-controller="Account" asp-action="SignUp">Register</a></li>
<li><a asp-controller="Account" asp-action="SignIn"><i class="fa fa-sign-in fa-fw" aria-hidden="true"></i>Login</a></li>
}
<li ng-cloak><span class="glyphicon glyphicon-shopping-cart"></span></i>Cart(<strong>{{CartCount}}</strong>)</li>
</ul>
I have a code like this for displaying menu, login, register and select language on the same line. How do I get the Language selectbox on the same line?
The missing code is implemented here.
Screenshot
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
#*<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">xxxxx</a>*#
<a class="navbar-brand" href="/WebZenter" tabindex="-1">
<span style="padding-left: 30px">xxxxxx</span>
<img alt="Brand" src="~/CustomerFiles/BeoTjenester/WebZenter50x50.png" width="20" height="20" style="margin-top: -20px;" />
</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="xxxxxxxx" asp-action="Jobs">xxxxxh</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">DinnerOut<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a asp-area="" asp-controller="DinnerOut" asp-action="Index">Create Bill</a></li>
<li><a asp-area="" asp-controller="DinnerOut" asp-action="Select">Select Bill</a></li>
</ul>
</li>
</ul>
#if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
<ul class="nav navbar-nav navbar-right">
<li>
<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">#Localizer["Hello"] #UserManager.GetUserName(User)!</a>
</li>
<li>
<button type="submit" class="btn btn-link navbar-btn navbar-link"><span class="glyphicon glyphicon-log-out"></span>Log off</button>
</li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Register"><span class="glyphicon glyphicon-user"></span> #Localizer["Register"]</a></li>
<li><a asp-area="" asp-controller="Account" asp-action="Login"><span class="glyphicon glyphicon-log-in"></span> #Localizer["Login"]</a></li>
<li>
<div title="#Localizer["Request culture provider:"] #requestCulture?.Provider?.GetType().Name" class="collapse navbar-collapse">
<form id="selectLanguage" asp-controller="ChangeLanguage"
asp-action="SetLanguage" asp-route-returnUrl="#Context.Request.Path"
method="post" class="form-horizontal" role="form">
#Localizer["Language"] <select name="culture"
onchange="this.form.submit();"
asp-for="#requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
</form>
</div>
</li>
</ul>
}
</div>
</div>
Missing code is added.
Goal: Display on mobile the Logo + Navbar items onto the same line. (I don't want these Navbar items inside the navbar-toggle collapsed)
1) The icons('search', 'message', 'globe') began hidden in the navbar-toggle collapsed
[navbar-toggle collapsed][1][1]: https://i.stack.imgur.com/LjtPr.png
2) The icons('search', 'message', 'globe') have been extracted from the navbar-toggle collapsed, but I've been unable to display them inline with the branding.
[Icons extracted from navbar-toggle collapsed][1] [1]: https://i.stack.imgur.com/5eomO.png
Note on img two:
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
Has been replaced with 'user-o' fa icon.
The following bootstrap navbar is used:
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link <span class="sr-only">(current)</span></li>
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
<li role="separator" class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>Link</li>
<li class="dropdown">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>