I'm very new to HTML and I've been trying to get my dropdown to work for hours. I searched many posts facing similar problem but still can't find and solve the problem. I'm not sure if the problem is also caused by my script
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <header class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#" id="logo">Logo</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"> <span class="navbar-toggler-icon"></span> </button> <nav class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item"><a class="nav-link active" href="#">Home <span class="sr-only">(current)</span></a></li> <li class="nav-item"><a class="nav-link" href="#">Link</a></li> <li class="nav-item"> <div class="dropdown"> <a href="#" class="nav-link dropdown-toggle" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a href="#" class="dropdown-item">Action</a> <a href="#" class="dropdown-item">Another action</a> <div class="dropdown-divider"></div> <a href="#" class="dropdown-item">Something else here</a> </div> </div> </li> <li class="nav-item"><a class="nav-link" href="#">Disabled</a></li> </ul> </nav> </header>
Thanks in advance!
I tried replacing data-toggle="dropdown"
with data-bs-toggle="dropdown"
but it doesn't work because I'm using bootstrap 4, directly Copy and paste the code from bootstrap and it still fails
CDN does not work with your jQuery. Bootstrap requires it to run. You got a clue in the console, there should be an error saying it requires jQuery. So you can:
Replace this
https://ajax.googleleapis.com /ajax/libs/jquery/3.5.1/jquery.min.js
With this
https://code.jquery.com/jquery-3.6.1 .min.js