Button to toggle the collapse of the Bootstrap navbar is unresponsive

My bootstrap navbar is not collapsing, even though the toggle button appears. When clicked, nothing happens. I'm using React so I'm using className instead of class. I've tried copying directly from the Bootstrap example and also from a Stack Overflow question (linked here) but it still doesn't work. Could I be missing an import somewhere?

<nav className="navbar navbar-default navbar-fixed-top navbar-shrink">
    <div className="container">
        <div className="navbar-header page-scroll">
            <button type="button" className="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar=in-collapse" aria-expanded="false" aria-controls="navbar">
                <span className="icon-bar"></span>
                <span className="icon-bar"></span>
                <span className="icon-bar"></span>
            </button>
            <a className="navbar-logo page-scroll" href="#about">
                <img src="icon.png" />Page
            </a>
        </div>

        <div className="collapse navbar-collapse" id="navbar-in-collapse">
            <ul className="nav navbar-nav navbar-right">
                <li className="hidden active">
                    <a href="#page-top"></a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#portfolio">Portfolio</a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#blog">Blog</a>
                </li>
                <li className="">
                    <a className="page-scroll" href="#contact">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

This is how it looks in my html:

<!DOCTYPE HTML>

<html>

<head>
  <title>Kaisin Li</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="./index.css" rel="stylesheet" />
  <link rel="icon" href="./favicon.ico">
</head>

<body>
  <div id="main"></div>
  <script src="./bundle.js" defer></script>
</body>

</html>

Any help would be appreciated!

Answer №1

To start, you must add links for jQuery and bootrstrap.js to your webpage. Next, replace

data-target="#navbar=in-collapse"
with
data-target="#navbar-in-collapse"
. Take a look at the code snippet below and give it a test run.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<nav class="navbar navbar-default navbar-fixed-top navbar-shrink">
    <div class="container">
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-in-collapse" aria-expanded="false" aria-controls="navbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-logo page-scroll" href="#about">
                <img src="icon.png" />Page
            </a>
        </div>

        <div class="collapse navbar-collapse" id="navbar-in-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li class="hidden active">
                    <a href="#page-top"></a>
                </li>
                <li class="">
                    <a class="page-scroll" href="#portfolio">Portfolio</a>
                </li>
                <li className="">
                    <a class="page-scroll" href="#blog">Blog</a>
                </li>
                <li class="">
                    <a class="page-scroll" href="#contact">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

If you are using bundles for jQuery and Bootstrap scripts, make sure they are bundled in the correct order.

Answer №2

Make sure to also add the bootstrap.min.js file.

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/5.0.1/js/bootstrap.min.js" ></script>

Answer №3

To utilize certain features in bootstrap, make sure to include the bootstrap js file.

Functions such as navbar collapse rely on this file, so it is essential for proper functionality.

For more information, refer to the fourth point here

Your code snippet should resemble something like this:

<!DOCTYPE HTML>

<html>

<head>
  <title>Kaisin Li</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link href="./index.css" rel="stylesheet" />
  <link rel="icon" href="./favicon.ico">
</head>

<body>
  <div id="main"></div>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script src="./bundle.js" defer></script>
</body>

</html>

Answer №4

When reviewing your code, please take note that there is a discrepancy between the data-target="#navbar=in-collapse" and id="navbar-in-collapse". One has '=' while the other has '-'. Double check for any spelling errors.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The jQuery statements are not properly executing the if and else conditions

i'm currently assessing the condition using this particular code. jQuery(document).ready(function($){ $('#uitkering_funds').hide(); $('#uitkering_funds_hoofd').hide(); $('#partner_uitkering').hide(); $('#par ...

utilize the flex index.html layout

Upon reviewing the template, I noticed that there is code implemented to check if the client has the necessary version. This code performs certain actions based on whether or not the required version is available. Additionally, there seems to be an <obj ...

Troubleshooting: Missing JQuery Header in Table

I'm currently working on creating a table using JQuery and everything seems to be functioning properly, except for one issue - the header is not displaying. I have defined the header, appended it to the table, and then added the column names to the he ...

What could be causing the lack of re-rendering in children components using redux-form?

When the parent component sends data, the children components do not re-render automatically. Re-rendering only occurs when a key is pressed on an input element. SMART User values from the state are sent by the smart component. If we add console.log(this. ...

What is the purpose of the .default() method being added to the class constructor in transpiled Typescript code

In TypeScript, I have the following code snippet to create an instance of ConnectRoles Middleware in Express: let user = new ConnectRoles(config); The middleware initialization is expected to be a simple call to a constructor. However, after transpiling, ...

The background color is showing the wrong shade

So, I recently created a simple HTML5 website and decided to set the background-color of the header div to #3D7D99. However, when I view it in the browser, it appears as #35728E. Can anyone explain what might be causing this discrepancy? UPDATE: Just to c ...

Displaying a success dialog after closing a Bootstrap modal

I have set up a bootstrap modal containing a form. Upon submitting the form, the form content is hidden and a bootstrap alert message is displayed. However, upon clicking the bootstrap popup, the same alert message is shown in the popup instead of displayi ...

Incorporate a checkbox within the DropzoneDialog for enhanced functionality

I am working with a DropzoneDialog and I want to add a checkbox to it when the dialog opens. Unfortunately, this is not functioning as expected. <DropzoneDialog acceptedFiles={['image/*', 'application/pdf', ...

Enhance your ReactJS application by integrating Bootstrap 4 for a sleek

Recently, I installed bootstrap 4 using yarn add, but now I'm facing issues with using components that rely on JavaScript as they require jQuery. Can anyone guide me on where to find the webpack file for adding files related to jQuery and Proper? My p ...

Ways to identify when all images have been loaded during a dynamic page load

I am trying to load a page with images into a div element. I need to find out when all the images on that dynamically added page have finished loading. $("#main").load('imagespage.php', function(){ alert("Page loaded"); }); The problem is that ...

Interactive drop-down menus created on the fly

I am currently working on dynamically generating inputs using Angular and Angular Material. Each time the user clicks on the Add button, a new dropdown should be created. However, I am encountering an error message that says: 'Error: [$parse:syntax ...

AngularJS component not functioning properly due to issues with 'bindings'

.component( 'testComponent', {bindings: {name:'<'}, template: `{{$ctrl.name}}<br/> {{$ctrl.title}}<br/> {{test.name}}<br/> {{test.title}}<br/> {{name}}& ...

"Utilizing React.js to implement white-space styling on numeric values for

I am a beginner in Reactjs and eager to learn and improve. Here is some code I have been working on: there's an <h1> element with the text "test", and beneath it, I want to display numbers vertically like this: 1:1, 1:2, 1:3. However, the CSS do ...

Link numerous number-type inputs together

I'm looking to create a form with 6 input fields of type number. Each label will have specified min and max values. The challenge is that if I set one field to be between 0-50, for example in red, then the next field in yellow can't be from 49-70 ...

Are there any comparable features in JavaScript and TypeScript that mirror Julia's metaprogramming and macros?

Having experience with Julia macros for metaprogramming, I find them to be a convenient way of generating versatile code. I'm curious if there is an equivalent approach in JavaScript or TypeScript. Is there a package or method that offers similar fun ...

Utilize nodemailer in Angular 6 to effortlessly send emails

I am currently experiencing an issue with my email service form in my Angular 6 application integrated with Node.js. I have set up the form using Bootstrap and Nodemailer for sending emails, but it seems to not be working as expected. Whenever I attempt to ...

What is the best way to implement conditional rendering for a button in a React component?

I am relatively new to using React and I have a question about adding conditional rendering to a button on my login page. I want the button to display a new component only when the entered username and password match the credentials stored in the API. How ...

Including a JavaScript file in an HTML document initiates a server request, which can lead to potential errors

I am currently developing a web application using Express and Node.js on the backend, with EJS as the templating engine on the frontend. Here is a snippet of my app.js file: app.get('/book/:id', (req, res)=>{ var book_id = req.params.id; cons ...

What is the best way to bypass routerlink usage in bootstrap tabs when working on an Angular application?

Attempting to configure basic HTML bootstrap tabs: <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" id="home-tab" dat ...

org.openqa.selenium.WebDriverException: unexpected issue: Chrome failed to initiate: crashed.(chrome inaccessible)

Having issues running Java script (selenium framework) on Chrome. Tried various solutions but still facing problems: Unchecked run as admin Added arguments Snippet of the code: ChromeOptions options = new ChromeOptions(); //options.setExperimentalOption ...