Issue with bootstrap: the navbar highlight and anchor href are not functioning as intended

I'm currently working on creating a Navbar and implementing the navbar highlighting feature using the active bootstrap class.

Below is the code I have tried:

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- Bootstrap CSS -->
  <link rel='icon' type='image/x-icon' href='static/images/logo.png'>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css"
    integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <title>FIFA Automation</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

</head>

<body>    
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">     
    <div class="collapse navbar-collapse flex-grow-1 text-right">
      <ul class="navbar-nav ms-auto flex-nowrap">
        <li class="nav-item active">
          <a class="nav-link external" href="/home" data-toggle="tab">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link external" href="/about" data-toggle="tab">About us</a>
        </li>
      </ul>
    </div>
  </nav>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/umd/popper.min.js"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
    crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
    crossorigin="anonymous"></script>    
</body>    
</html>

JS file:

<script>
    $(document).ready(function () {
      $(".navbar-nav .nav-link").click(function (e) {
        e.preventDefault();
        $('.nav-item').removeClass('active');
        $(this).closest('.nav-item').addClass('active');
      });
    });
  </script>

Included e.preventDefault(); to highlight the navbar, but this causes redirection to the href link not to work as expected.

If I remove this line, the redirection works but the navbar highlighting doesn't. How can I solve this dilemma?

Answer №1

Customize the names of classes to fit your needs.

 $(".menu .item-link").on("click", function(){
   $(".menu").find(".active").removeClass("active");
   $(this).addClass("active");
});

or

$(".menu-list").on("click", function(){
      $(".menu-item a").find(".active").removeClass("active");
      $(this).parent().addClass("active");
    });

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

Using RMarkdown to incorporate custom CSS styling in your documents

Having some trouble with my HTML report created from RMarkdown and applying CSS. The browser version displays correctly, but when printed, the styling doesn't come through. Below is an example of the RMarkdown code: --- title: "Table" output: html_ ...

Unable to create follow/unfollow feature with jquery ajax

Issue: While the database part for following and unfollowing actions is functioning correctly, there seems to be a problem with the jQuery and Ajax section. The follow button changes to unfollow (with some CSS styling) only after refreshing the page, rathe ...

Grab the contents of the header while excluding specific attributes

CSS <div [@class="another class"]> <p> Different text <span> yet more text </span> </p> </div> Is there a way to create a selector in CSS to style the text inside p that is not within the child eleme ...

MVC encounters difficulty generating content and script files following view addition

Encountering an issue with creating an MVC empty project and adding a View from the controller. The _Layout.cshtml file is generated, but content, script, and font files are not being created. Any suggestions for resolving this? https://i.sstatic.net/45Di ...

Is there a way to identify which elements are currently within the visible viewport?

I have come across solutions on how to determine if a specific element is within the viewport, but I am interested in knowing which elements are currently visible in the viewport among all elements. One approach would be to iterate through all DOM elements ...

Define the width of jqgrid

I have been utilizing SmartAdmin jqgrid to showcase data in tabular format. The number of columns in the datatable changes dynamically each time. I am converting the DataTable to a JSON object and assigning it to the jqgrid. However, it appears that jqgrid ...

Adjust the canvas size to fit its parent element ion-grid

I am currently working on an Ionic 3 component that displays a grid of images connected by a canvas overlay. I have utilized Ionic's ion-grid, but I am facing an issue with resizing the canvas. The problem is that I cannot determine the correct dimens ...

Trouble with displaying vertical scroll bar in React app when height is set to 100%

I am developing a React application where I need the content to always occupy at least 100% of the height to style the background accordingly. I have attempted to achieve this by setting the CSS height to 100% at the top level, which works fine. However, t ...

Tips for creating SCSS shorthand for an Array value color within a property

I've got a SCSS file containing an array of color values. export const COLORS = { transparent: 'rgba(0,0,0,0)', primary: '#09596e', primary01: '#317D92', primary02: '#6BADBF', primary03: '#97C6D2& ...

Is it Possible to Load Images Without Using Javascript?

When I hover over certain links on my HTML pages, large images are displayed but take a while to load. I prefer not to use JavaScript to preload the images. Are there any alternative solutions available? ...

Best practice for including the language tag in Next.js Head tag

I'm struggling to find a clear example of how to use the language tag with Next Head. Here are two examples I have come across. Can you help me determine which one is correct, or if neither are appropriate? Just a reminder: it doesn't need to be ...

increase the width of the side menu bar to accommodate more content on the Bootstrap

At the moment, the line that separates the content from the side menu stops at the side menu's content. However, the content is going to be longer than the side menu, causing the page to look uneven. I attempted to adjust the page-content d-flex but d ...

What alternative can be used for jquery isotope when JavaScript is not enabled?

Is there a backup plan for jQuery isotope if JavaScript isn't working? For instance, if I'm using the fitColumns feature, is there an alternative layout style available in case JavaScript is disabled, similar to what is seen on the new Myspace? ...

Loading event in HTML5 video element is in progress

I'm interested in creating a loading animation for an html5 video, similar to the display on Youtube videos (reference: https://www.youtube.com/watch?v=5vcCBHVyG50) I attempted using the canplay event but I believe I may have misunderstood its true p ...

How to Ensure an Element Appears Above Another Despite Z-Index Troubles?

After conducting approximately 2 hours of research on this topic, I was unable to find clear answers or solutions. Hence, I have decided to address the issue here. The problem I'm facing is as follows: Due to the nature of HTML/CSS, it seems impossi ...

Issues with table formatting and malfunctioning functions

I created a basic tic-tac-toe game, but I'm having an issue with the table display. It appears squished when the game loads. Is there a way to prevent this and keep the table empty? Additionally, I am using NotePad++ and encountering problems running ...

Positioning a flash element in the center

Struggling to find the perfect alignment for a flash widget on this particular page . Experimented with using both the <center> tag and various other methods, but alas, it remains stubbornly off-center. If anyone has any suggestions or solutions, p ...

Ways to determine if an element has exceeded its container's boundaries

After creating the codesandbox, I have developed a webapp that heavily relies on user input. To keep it simple for demonstration purposes, I am displaying various authors on an A4 formatted page using `page` and `font-size` with the `vw` unit for responsiv ...

Is it advisable to incorporate Material-UI into a React project?

When it comes to designing a login page in react, I stumbled upon material-ui. The real question is, should we utilize Material-UI for this purpose? Furthermore, how can we manage styles in a separate file in the given examples? It seems logical to place t ...

Having trouble applying CSS to multiple classes used in an AJAX call

I'm trying to utilize richfaces for some fast AJAX widgets, but I am encountering difficulties in setting CSS parameters for them. After inspecting the generated code, I found that the elements have the classes "rf-ds" and "rpds". Despite applying st ...