What could be causing the issue with my Bootstrap Scrollspy?

I'm sure you're familiar with CodePen questions.

Here's mine: https://codepen.io/fender90/pen/KqVLba

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

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

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">

...

<p>CSS</p>

<pre><code>* {
overflow: auto;
}

body {
position: relative;
height: 100%;
overflow: auto;
}

.fa, .navbar, .navbar-inverse {
overflow: hidden;
}

.row, .about-me {
display: flex;
height: 100%;
}

.navbar-inverse {
font-size: medium;
padding: 10px;
}

...

<p>jQuery</p>

<pre><code>$(window).load(function() {
var navheight = parseFloat($(".navbar").height());
$("body").css({ "margin-top": navheight + 20 + "px" });
});

Why doesn't the scrollspy work as expected? Elements in the navbar are not highlighted while scrolling.

I've tried following instructions from this link.

I also attempted using:

$('body').scrollspy({target: "#navbar"})

But still no success. Any ideas?

Appreciate your help!

Answer №1

If you happened to notice the console, it will display:

Bootstrap's JavaScript requires jQuery version 1.9.1 or higher, but lower than version 4

The issue arises from using an older version of jQuery. Update to a higher version and everything should function correctly.

In the example below, I have utilized v3.1.1 [Go fullscreen and check]:

CodePen Link

Please make sure to correct the following line as well:

<nav class="navbar navbar-inverse navbar-fixed-top" id="#navbar">

Change it to:

<nav class="navbar navbar-inverse navbar-fixed-top" id="navbar">

The use of the # symbol should only be in the data-target, not within the actual id attribute of the element.

Answer №2

One issue you may be facing is with the way you name your CSS classes and IDs.

It's best to use classes (denoted by a period) for non-unique elements that will be repeated throughout your code.
IDs (denoted by a hashtag) should be used for unique elements.

My hunch is that the problem lies in the browser struggling to differentiate between non-unique IDs when navigating through sections of your code.

I will update this response once I discover the solution.

Update: The answer has already been provided in this thread.

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

Selecting attributes from HTML strings using jQuery is a common task

I have a jQuery string that includes HTML code. btn_response = '<input type="button" id="upload-btn5" style="margin-bottom: 7px;" class="btn btn-success clearfix" value="Choose file">'; The id attribute changes each time, so I am wonderin ...

Display or conceal numerous WTForm labels

Is there a more efficient way to hide/show multiple wtform labels? Currently, I am achieving this with the following code: HTML: {{ render_field(var_1, class = "class_1") }} {{ render_field(var_2, class = "class_1") }} {{ render_field(var_3, class = " ...

Receive alerts from flash document within html

I have a flash file embedded in an html document using the embed tag. I am looking for a way to trigger a JavaScript action, such as an alert, once the Flash content has finished playing. Is there a method to achieve this? ...

Select2 isn't functioning properly, consistently showing the message "No results found" every time

After exhausting all options on this platform without success, my goal remains unchanged: to extract state data from a JSON file generated by a servlet and then present the information in a dropdown menu using AJAX. However, no matter what I input into th ...

Using jQuery to send information to a freshly opened tab and retrieve the response

I'm attempting to transfer information from ClassA to ClassB. To achieve this, I've set up a hidden form within the view page of ClassA and submitted it to ClassB. <form id="invisible_form" action="classB" method="post ...

Broadening the capabilities of jQuery through a versatile function

Currently, I am in the process of developing a generic function for my website using jQuery that will be utilized across the entire site to display success or error messages. After careful consideration, I have decided to transform this function into a plu ...

How can I position an element at the bottom of a page using VueJS and Bootstrap CSS?

https://i.sstatic.net/ojanG.png I am trying to move my footer (Copyright statement) to the bottom of the page. However, it is creating some extra white space beneath the footer. How can I eliminate this additional white space? This issue is occurring in ...

Is it possible for Apache PHP looping to cause delays for other users trying to access the server?

Currently, I have incorporated Phalcon as the framework for my server development and am encountering an issue while trying to implement a loop within a function. Let's say a user provides input on the page, which is then sent via ajax to a specific ...

I'm encountering an issue where the data in my personalDeatail model's add value is not updating automatically. Can someone please help me

I've been struggling to automatically update the data in model personalDetail.add value when adding two column data. The added data appears correctly in the input box, but it's not updating in personalDetail.add. What am I missing here? Help need ...

Jquery - Ajax: Error in Syntax JSON.parse: character not expected

I am encountering an issue with parsing JSON data on my client-side code. The JSON received from the server looks like this: [{"name":"Bubble Witch Saga 2","impressions":10749},{"name":"Grinder","impressions":11284},{"name":"Loovoo","impressions":12336},{" ...

Tips for creating two columns within a Bootstrap row, one with a form and the other with an image, that are both the same height and responsive

I am facing an issue with my bootstrap columns. One column has a form, while the other contains an image. The image section is set to 100% width, making it responsive when the screen size changes. However, the form remains at a fixed height regardless of t ...

Encountering a problem in my PHP MySQL and AJAX implementation. Attempting to retrieve data from a database based on a specific variable representing a

After coming across a helpful example on w3schools.com about php, mysql, and ajax, I decided to customize the code for my project. However, I've run into a roadblock as I can't seem to display the information extracted from mysql. I'm uncert ...

How to Trigger a Function in Backbone.js Upon Page Load

My function uses the simple selector $(.some-class) instead of this.$(.some-class). When I call this function in the render, it does not find the .some-class. It seems like the function needs to be called when the DOM is fully loaded. How can I achieve tha ...

Preventing clashes in namespaces while incorporating Bootstrap into a plugin for WordPress

After creating a WordPress plugin, I am considering revamping its layout with the help of bootstrap CSS and js. However, I have encountered issues in the past due to conflicting CSS naming conventions. I want to ensure that there are no namespace conflicts ...

What is the procedure for extracting data from a JSON file within an HTML document?

Hey there, I am currently working on reading data from a json file using ajax. I have created a Video object that consists of Courses objects with titles and URLs. However, when attempting to read the title and URL of HTML as an example, I am not seeing a ...

What methods are recommended for expanding the size of a select/dropdown menu?

Managing a long list of values can be challenging, especially when it continues to grow. Consider the following example: <select> <option value="1">1, some test</option> <option value="2">2, some text</option> <optio ...

How to submit a form in Spring MVC without causing page refresh

Is there a way to keep the data in listbox2 after submitting the form? Currently, when I submit the form, I lose the data in listbox2 and have to reselect everything again. <form:form action="choseElement" method="post"> <select name="listbox1 ...

Obtaining a background image within a keyframe

I have been experimenting with keyframe animations and am looking to incorporate multiple images into the animation. Specifically, I want to switch out the image every 10% increment to depict someone running. Despite my efforts to use a background-img in ...

Adjust the element's height as you scroll

I am currently experimenting with a method to dynamically increase the height of an element based on user scrolling behavior. Despite trying multiple approaches, I have encountered challenges in getting it to work effectively. The concept is as follows: ...

Are there any disadvantages or more efficient options for using jQuery to call .NET Page Methods?

I am currently exploring a more efficient approach to handling ajax calls in our existing .net webforms app. I have a solid understanding of jQuery and find it to be quite useful. My idea is to suggest to my team the utilization of jQuery for direct callin ...