What's the issue with the addClass function not working as expected?

I am currently integrating the website's navbar using PHP, which is why I am unable to use class="active" to highlight the active tab on my navigation bar individually. To solve this issue, I attempted to add the active class to the corresponding tab using a small jQuery code snippet. However, despite my efforts, it does not seem to be working and I have been unsuccessful in finding a solution.

Here is the jQuery code I used:

<script>
$(document).ready(function(){
  $("#nav_about").addClass("active");
});
</script>

Below is the HTML code for my navbar:

<nav class="navbar navbar-default navbar-sticky">
<div class="container-fluid">
<div class="navbar-header">
   <a class="navbar-brand hidden-sm" href="#myPage">Logo</a>
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav navbar-center">
                <li cl ass="active"><a href="index.php">Home</a></li>
                <li id="nav_about"><a href="about.php">About</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> TEAM <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="teachers_panel.php">Teachers</a></li>
                        <li><a href="students_panel.php">Students </a></li>
                    </ul></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> Events <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="dropdown-header">Upcoming Events</li>
                        <li><a href="sidebar-right.php">1st ACC IT Carnival 2016</a></li>
                        <li class="dropdown-header">Past Events</li>
                        <li><a href="cgc.php">Inter House Cyber Gaming Competition 2015</a></li>
                        
                        /* additional event links */

                    </ul></li>  
                    <li><a href="u_cons.php">Gallery</a></li>
                    <li><a href="u_cons.php">Get Involved</a></li>
                    <li><a href="contact.php">Contact</a></li>
                </li>
            </ul>
   </div>
 </div>
</nav>

Edit:

I have corrected the errors in my code, including missing parentheses after $(document).ready(.. and adding a dollar sign before (document).

Answer №1

It appears that there are some errors in your HTML and JS code. Here is the corrected version:

$(document).ready(function(){
  $("#nav_about").addClass("active");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-default navbar-sticky">
<div class="container-fluid">
<div class="navbar-header">
   <a class="navbar-brand hidden-sm" href="#myPage">Logo</a>
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav navbar-center">
                <li cl ass="active"><a href="index.php">Home</a></li>
                <li id="nav_about"><a href="about.php">About</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> TEAM <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="teachers_panel.php">Teachers</a></li>
                        <li><a href="students_panel.php">Students </a></li>
                    </ul></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> Events <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="dropdown-header">Upcoming Events</li>
                        <li><a href="sidebar-right.php">1st ACC IT Carnival 2016</a></li>
                        <li class="dropdown-header">Past Events</li>
                        <li><a href="cgc.php">Inter House Cyber Gaming Competition 2015</a></li>
                        <li><a href="tbtt.php">Take Back the Tech </a></li>
                        <li><a href="gbb.php">Google Bus Bangladesh at ACC </a></li>
                        <li><a href="moz.php">Mozcoffee at ACC </a></li>
                        <li><a href="hoc.php">Hour Of Code </a></li>
                        <li><a href="hjfp.php">Club Hangoutat JFP </a></li>
                    </ul></li>  
                <li><a href="u_cons.php">Gallery</a></li>
                <li><a href="u_cons.php">Get Involved</a></li>
                <li><a href="contact.php">Contact</a></li>                    
            </ul>
   </div>
 </div>
</nav>

Answer №2

If you're using jQuery, make sure to use $(document) instead of just document.

$(document).ready(function(){
  $("#nav_about").addClass("active");
});
.active {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>

</script>
And here is my navbar html:

<nav class="navbar navbar-default navbar-sticky">
<div class="container-fluid">
<div class="navbar-header">
   <a class="navbar-brand hidden-sm" href="#myPage">Logo</a>
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav navbar-center">
                <li cl ass="active"><a href="index.php">Home</a></li>
                <li class="active" id="nav_about"><a href="about.php">About</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> TEAM <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="teachers_panel.php">Teachers</a></li>
                        <li><a href="students_panel.php">Students </a></li>
                    </ul></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> Events <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="dropdown-header">Upcoming Events</li>
                        <li><a href="sidebar-right.php">1st ACC IT Carnival 2016</a></li>
                        <li class="dropdown-header">Past Events</li>
                        <li><a href="cgc.php">Inter House Cyber Gaming Competition 2015</a></li>
                        <li><a href="tbtt.php">Take Back the Tech </a></li>
                        <li><a href="gbb.php">Google Bus Bangladesh at ACC </a></li>
                        <li><a href="moz.php">Mozcoffee at ACC </a></li>
                        <li><a href="hoc.php">Hour Of Code </a></li>
                        <li><a href="hjfp.php">Club Hangoutat JFP </a></li>
                    </ul></li>  
                    <li><a href="u_cons.php">Gallery</a></li>
                    <li><a href="u_cons.php">Get Involved</a></li>
                    <li><a href="contact.php">Contact</a></li>
                </li>
            </ul>
   </div>
 </div>
</nav>

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

Tips for dynamically loading a Jquery plugin/script when the page changes on Vue router

After searching for solutions on this platform, I have not found any that work for my specific issue. If you want to see a working example, you can check out this demo. My project is quite straightforward - it's a one-page application with Vue handl ...

Exploring the art of CSS box-shadow and playing with margins

In one of my blog posts, I've applied CSS box-shadow to the <img> elements. To ensure that they adjust their size along with the column, I have set max-width:100%. However, the box-shadow spills over into the margins due to its rendering outsid ...

I can view this email signature correctly in VS Code and my browser, but all the other applications I've tried so far are unable to display it correctly

Simply put, I am facing an issue with my email signature. I use eM Client, while my client uses Outlook and I've also checked it in iCloud webmail. Surprisingly, none of these platforms display my email signature correctly, but VS Code and all my brow ...

Jquery debugger keeps getting triggered multiple times by checkbox interactions

I've encountered an issue while working on Mvc.Grid that involves the onclick event of a checkbox. Within the grid, each row represents a Profile and contains a checkbox for selection. My goal is to retrieve a list of IDs corresponding to the checked ...

Looping through multiple ajax requests to track the most recent responses

Run a loop with the most recent response obtained from the previous ajax call. The current code is continuously displaying the same response. PHP outputs data in text format. Currently, the code fetches response text from the PHP file and appends it to a ...

IE9 does not trigger the jquery ajaxStop event

Our team is currently working on hiding a loader icon once multiple async ajax calls have been completed. To achieve this, we are attempting to utilize the ajaxStop event. However, we have encountered an issue where this event does not seem to be firing in ...

Creating a centered arrow using CSS within boxes

Here is the CSS class that I have applied to my divs: .rightarrow{ width: 0; height: 0; border-style: solid; border-width: 10px 0 10px 20px; border-color: transparent transparent transparent #aaaaa9; } This is ...

Exploring the Concept of Nested ViewModels in Knockout.js Version 3.2.0

I have a global view model that is applied to the main div and I also have other view models that I want to apply to nested elements within my main div However, I am encountering an issue: You cannot bind multiple times to the same element. Below is ...

What is the proper way to mention "a" within an unsorted list?

I currently have a div section with the following content: <ul id = "issues"> <li> <h1>title</h1> <p>text text text </p> <a class="next" href="#">next</a> </li> <li> <h1>title</h1> &l ...

How can I remove HTML tags from a string using .NET?

Does anyone know of an efficient way to strip HTML tags from a string of HTML text? ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

`Incompatibility with Internet Explorer causes AJAX loader GIF to fail when using asynchronous POST requests`

Is there a way to display an AJAX loader gif during an asynchronous POST request on Internet Explorer? It seems that the request process stalls and the updated content is not visible when using Internet Explorer. However, everything works fine on browser ...

Reset the dropdown list back to its initial state

I am facing an issue with two dropdown lists in my view. When I change the value on the first one, it should update the second one accordingly. Initially, this functionality works fine with the script provided below. However, if I change the first dropdown ...

Incorporating the JQuery plugin Lightbox_me into a Ruby on Rails application

Greetings! I am currently attempting to incorporate a popup window using the Lightbox_me plugin in my Ruby On Rails application. After downloading jquery.lightbox_me.js and placing it in the app/assets/javascripts directory, I added //= require jquery.lig ...

Transferring HTML content using jQuery AJAX and PHP

Can anyone help me with displaying the content of a division on one page to another? <div id="box-cont" class="box-content"> <?php echo $stat;// Includes multiple images and s ...

Disallowing selection on input text field

I am seeking to disable selection on an input field while still allowing for focusing, with the following criteria: Preventing selection via mouse Preventing selection via keyboard (including Ctrl+A, shift+arrows) Permitting focus on the field using both ...

Locate and eliminate all occurrences of "<br />" within a webpage using jQuery

I am currently working on a project using the Cargo Collective platform and I have noticed that it automatically includes unnecessary <br /> tags in the body of my document. Is there a way to use jQuery to scan for all instances of <br /> tags ...

When adding text to a React Material UI Box, it can sometimes extend beyond the boundaries

I'm currently working on a straightforward React page, but I've encountered an issue right from the start: my text is overflowing the box and I can't figure out why. Could someone take a look at my styles? Here's a picture showing the ...

Is there an issue with the jQuery ajax function when it comes to sending the RegistrationId to our server?

Beginning to work with the pushNotification service for my Android app, I successfully received a registration ID from the GCM server and attempted to send this ID to our server using a jQuery AJAX function. My intention was to send this ID after the user ...

What is the best way to implement dragging functionality for an image within a specific area?

Here's a snippet of the code I'm working with: link This is the HTML code: <div class="border"> <div id="i-choose"> <input type="file" name="file" id="file" class="inputfile"> <label for="file"& ...