Error in the code that I am unable to locate in JavaScript, HTML, and CSS

I need help creating a navbar that displays the active site using HTML and JS code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/style.css">
    <title>ANKAR - Website Version</title>
</head>
<body>
    <div class="banner">
        <nav>
            <h4 class="logo">ANKAR</h4>
            <ul class="nav-links">
                <li><a class="active" href="#">Home</a></li>
                <li><a href="#page2">Page</a></li>
                <li><a href="#">Page</a></li>
                <li><a href="#">Page</a></li>
            </ul>
            <div class="burger">
                <div class="line_1"></div>
                <div class="line_2"></div>
                <div class="line_3"></div>
            </div>
        </nav>
        <section class="front_page">
        <div class="heading">
            <h1>ANKAR</h1>
            <p>Where do you want to go, Pawel?</p>
            <div class="cta">
                <button class="star_wars">Star Wars</button>
                <button class="ld">Lucid Dreaming</button>
                <button class="ba">Add pls</button>
            </div>
        </div>
    </section>
    </div>
    <script src="../static/java_file.js"></script>
    <script>
        $(document).ready(function() {
            $( "nav ul li a.active" ).bind( "click", function(event) {
                event.preventDefault();
                var clickedItem = $( this );
                $( "nav ul li a.active" ).each( function() {
                    $( this ).removeClass( "active" );
                });
                clickedItem.addClass( "active" );
            });
        });
        </script>
</body>
</html>

There seems to be an issue with the code as it is not functioning properly. I am not proficient in JavaScript so I have tried modifying the code by changing the "nav ul li a.active" components but it still doesn't work. Can someone please provide some guidance?

CSS:
nav ul li a.active::after {
    background: linear-gradient(90deg, grey 30%, #fff 0%, #fff 80%);
    content: "";
    height: 3px;
    width: 100%;
    position: absolute;
    left: 0;
    bottom: -10px;
    box-shadow: 15px 0px 5px 2px rgba(0,255,255,1);
    clip-path: inset(-10px -0.5px -10px 2px);
}

Answer №1

Ensure that the click event is bound to all navigation links, not just the 'active' ones:

           $( "nav ul li a" ).bind( "click", function(event) {
                event.preventDefault();
                var clickedItem = $( this );
                $( "nav ul li a.active" ).each( function() {
                    $( this ).removeClass( "active" );
                });
                clickedItem.addClass( "active" );
            });

Answer №2

Utilize jQuery, a powerful JavaScript library

To ensure functionality, add the following code snippet at the beginning of your <head>:

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

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

Exploring nested static properties within TypeScript class structures

Check out this piece of code: class Hey { static a: string static b: string static c: string static setABC(a: string, b: string, c: string) { this.a = a this.b = b this.c = c return this } } class A { static prop1: Hey static ...

Maintain Open State of Toggle Drop Down Menu

Having an issue with the toggle down menu or list on my webpage. The problem is that the menu is constantly open (the #text_box) when the page loads. My intention was for it to be closed initially, and then open only when the user clicks on the 'Ope ...

Tips for positioning an inline label and input field in an Angular application using CSS and HTML: How to align the label to the left and the input

I'm currently developing an Angular form with multiple label-input field combinations. I have managed to style the labels and input fields with inline block so that they appear on the same row. However, I am facing a challenge in aligning the label to ...

Trouble with jQuery: Background color fade in/fade out effects not functioning

Issue with changing background color during FadeIn and FadeOut effect. Whenever I click on the "whatup" link, my button should blink with alternating red and white colors. Below is the HTML code: <a id="blkwhatsup" href="#" >whatup</a> <in ...

Reordering a pair of items within an array using ReactJS

After pondering, I wondered if there exists a neat and tidy method to swap two objects within an array while utilizing setState. Here's my current approach: export function moveStepUp(index) { if(index > 0){ let currentStep = this.stat ...

Batch Script for Reading and Writing HTML files

My experience with writing DOS Batch scripts has been limited to basic tasks. However, I recently encountered a need to create a script that reads an HTML template file and generates a new file from it. I successfully managed to store the file data in a va ...

Ensuring the Accuracy of POST Parameters Using Express-Validator

I've been working on adding parameter validation to my Node/Express API by utilizing express-validator. However, I encountered a situation where even though I sent a POST request with a missing "name" field using the curl command curl -X POST -d "foo= ...

Collaborating on validating inputs and modules for both backend and frontend

Sharing input validation is crucial for various reasons: Immediate feedback to users on the frontend regarding the validity of their input Enhanced security measures in the backend, preventing manipulation of data through the API Dependency on frontend J ...

Tab button that can be easily printed

I'm currently facing a challenge with my HTML code on my website. I have 5 tabs already set up, but I want to add one that redirects users to a printable page that opens the print menu specific to their browser. It seems like there might be an issue i ...

Searching for the row value of specific data within a table using Javascript

I have a PHP table populated with values, and I want to make two of the table data cells editable. The table headers include: Task, Due Date, Staff, and Department. The values in the table data are retrieved from a database using Laravel Eloquent. &l ...

Save to clipboard HTML with forward slash

I need help creating a button that will copy a specific text to the clipboard when clicked. <button onclick="copyToClipboard('A\B\C\D')">Click here</button> However, I've encountered an issue where the text copie ...

Using setAttribute will convert the attribute name to lowercase

When creating elements, I use the following code: var l = document.createElement("label");. I then assign attributes with l.setAttribute("formControlName","e");. However, a problem arises where the setAttribute method converts formControlName to lowercase ...

Ways to eliminate additional container padding within flexbox

I'm struggling with understanding flexbox and couldn't find any clear explanation on the W3 page about how to remove the extra spacing between containers. For example, in one of their demos, I'm unsure how to achieve a layout similar to lef ...

Trigger price update in jquery based on radio or checkbox selection and specific conditions

https://i.sstatic.net/OIvgF.png In my product form, I have implemented a feature where selecting the "product type" and "product size" will update the price. However, if the user changes the product type after selecting the size, the price does not update ...

What is the best way to access the attributes of a particular object following a triggered event?

I'm a bit unsure if my title makes sense, so allow me to explain. In a nutshell, I have 2 li elements within a ul list. What I aim for is that when one of these li elements is clicked, the text within that particular li is retrieved, and then an obje ...

The error message "Evaluating this.props.navigation: undefined is not an object" indicates that there

In the navigation bar, I am trying to keep a touchable opacity at the top right. When this touchable opacity is pressed, I want to redirect the user to the home page. constructor(props) { super(props); this.state = { stAccntList: [], ...

What is the best way to verify the success of two ajax requests?

There are two ajax requests being made to check the availability of two images. Below is the code for these ajax calls: if (result['img_one'] !== '') { var src_one = "blah-blah-one.jpg"; $.ajax({url: src_one, success: ...

Tips for altering the background of a video on Vonage

Hello everyone, Currently, I am incorporating the Vonage API for video calling into my project. I would like to tweak the video background - does anyone have any ideas on how to accomplish this? I eagerly await your responses! Thank you in advance! ...

The text box is intersecting with the photo

Struggling to craft a clean layout with an upvote button, I encountered an issue where the textarea appears to overlap with the image. Below is my sample code snippet: .my-component-row { display: flex; justify-content: center; align-items: center ...

npm ERROR! The module './access-error.js' could not be located

I keep encountering an issue with NPM where I always get the error message npm ERR! Cannot find module './access-error.js'. Can anyone provide assistance? The problem initially arose when attempting to install vue-chartjs. Following the document ...