Ways to emphasize the currently active tabs on a navigation bar

How can I highlight the active tabs and shift it when clicking on another link?

$(function() {
  $('.nav ul li').on('click', function() {
    $(this).parent().find('li.active').removeClass('active');
    $(this).addClass('active');
  });
});
.nav {
  background-color: #510A32;
  height: 120px;
  background: cover;
  margin: 0;
  padding: 0;
  overflow: hidden;
  top: 0;
}

.nav ul {
  list-style-type: none;
}

.nav li {
  float: left;
  padding: 30px 40px 70px 0px;
  font-family: Arial;
}

.nav li a {
  display: block;
  color: #fff;
  padding: 14px 16px;
  text-decoration: none;
  font-weight: bold;
}

.nav ul li:hover a {
  color: gold;
}

li.active a {
  color: gold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="nav">

  <ul>

    <li class="active"><a id="home" href="#">Home</a></li>

    <li><a id="story" href="#">Our Story</a></li>

    <li><a id="Menus" href="#">Menus</a></li>

    <li><a id="OrderOnline" href="#">Order Online</a></li>

    <li><a id="contact" href="#contact">Contact & Reservations</a></li>

  </ul>

</div>

Answer №1

Try using this method, it may help solve your issue

ul li a.active {
  color:blue;
  background:#ccc;
  text-decoration: underline;
}

Include your HTML code below...

<ul>
  <li><a href="#" class="active">Home</a></li>
  <li><a href="#" >Services</a></li>
</ul>

Add your Javascript code here

document.querySelector("a").addEventListener("click", function () {
    this.classList.toggle("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

Performing a jQuery ajax request on data that has been dynamically loaded using

I am currently in the process of constructing a form for users to make reservations on a website. I have successfully set it up so that when a user selects a shift, the available dates are populated. Now, my goal is to have the available seats load when a ...

Utilize jQuery AJAX POST to Submit a File

I am currently facing a challenge in uploading a file to a server using C#. To provide some context, I have successfully implemented this functionality using jQuery/PHP. With jQuery/PHP, I can smoothly execute my AJAX request and transmit the uploaded fil ...

ASP.Net MVC page is sending data to the controller without displaying the record ID in the address bar

In my web application, I have implemented a Razor page to display a list of meetings. Each meeting has a unique ID which is associated with a link. Clicking on this link loads another Razor view that shows the details of the selected meeting. Within this v ...

Reading inputs from a Text Area using ko.JS databinding - Issue with capturing new lines

This is the code I have written for a text area: <div> <textarea data-bind="value: environment" rows="10" cols="20" class="form-group"></textarea> </div> I am looking to show the content typed in this text area in another DIV. ...

Guide to Wrapping Inner or Wrapping All Elements Except for the Initial Div Class

I am looking to enclose all the elements below "name portlet-title" without including other elements within the "div class name portlet-title". I have attempted the following methods: 1) $("div.item").wrapAll('<div class="portlet-body"></div ...

Invoke functions within a separate function

I am facing an issue when it comes to invoking functions within another function. Below is a snippet of the code I am working with: <script> function saveInfo() { function returnEmail() { var _e = document.getElementById("em ...

What is the process for obtaining the URL of the website that is hosting my iframe?

Do you have a technical inquiry? I am curious to know if it is feasible to retrieve the URL of the website that is hosting my iframe. The pages that host my iframe are utilizing the following code: <iframe id="vacancy-iframe" src="http://mypage.co ...

After the video finishes playing, a black screen is displayed on the HTML 5 video

I'm facing an issue with a video loaded in the <video> tag. The video is set to play only once and doesn't loop. However, after the video finishes playing, it just displays a black screen. I've attempted to use the poster attribute, ...

Saving the subtracted value from a span into a cookie until the checkbox is unchecked - here's how to

I am working on a piece of code that includes numeric values within a span. When the checkbox is clicked, it subtracts 1 from the main value, essentially reducing the total value. How can I achieve this so that even after the form is submitted, the deducte ...

Tips for preserving HTML markup in a Google Sheets cell without disrupting the tags

I am currently experimenting with using Google Sheets to input the complete HTML of an email into a cell, where I have variables for text, image src, href src, and more. As of now, I am utilizing Google Script to substitute tags with the corresponding val ...

Best Way to Eliminate "#" Symbol from URL Address in UI-Router

My website URL is structured as follows: This is the index page where I utilize Angular UI-Router to navigate to different views, however, the URL retains the hash symbol (#) like this: Query: I am looking for a way to eliminate/remove the hash tag from ...

Background image loading gets delayed causing it to flicker

Instead of having separate files for different elements on my site, I decided to keep all the JavaScript in one large file called my_scripts.js. To speed up loading times, I defer the loading of this file using inline JavaScript and make sure it is the las ...

Tips for concealing the "infoFiltered" feature in the jQuery datatable plugin

Within the datatable plugin, there is an option called "infoFiltered" which displays the total number of rows in the datatable across all pages. However, I am looking to hide this information. Is there a way to achieve that? ...

What is the best way to loop through ng-repeat with key-value pairs in order to display each

I need to loop through and show the data stored in "detailsController". <div ng-controller="detailsController"> <div ng-repeat="data in details" id="{{data.Id}}"> {{data.Name}} </div> </div> ...

Utilize Sass variables to store CSS font-size and line-height properties for seamless styling management

How can I save the font size and line height in a Sass variable, like so: $font-normal: 14px/21px; When using this declaration, I notice that a division occurs as explained in the Sass documentation. Is there a way to prevent this division from happening ...

Is there a way to change a string that says "False" into a Boolean value representing false?

When retrieving values from the backend, I am receiving them as strings 'True' and 'False'. I have been attempting to convert these values into actual Boolean values, however, my current method always returns true. What is the correct a ...

The form includes Bootstrap columns and a button, but there is noticeable wasted space within the

This is the form structure I am working with: <form method="GET" action="."> <div class="container-fluid pt-2 px-5 py-3"> <div class="row g-4"> <div class= ...

What is the best way to position a button under an h3 heading using the display flex

Inside my div, I have an h3 and a button. The div uses display:flex; with justify-content:center; and align-items:center;. However, the button ends up sticking to the right side of the h3. I attempted placing the button in its own div, but this caused a la ...

CSS stylized horizontal navigation bar completed

* { border: 0px; margin: 0px; padding: 0px; } body { background-color: #FFC; } #wrapper { width:70%; margin: 0% auto; } #header { background-color: #C1D1FD; padding: 3%; } #nav { clear:both; padding: auto; position:inherit; background-color:#F0D5AA; wid ...

Guide on using Python to save a legitimate HTML page of a YouTube video

When I use requests.get(URL, allow_redirects=True) on websites like Youtube or Reddit, instead of seeing the usual HTML text content that appears when I view the page in a browser, I get a bunch of unexecuted JavaScript. All I really need is the title of ...