pointing out the existing issues with the menu

I am struggling to figure out why I cannot highlight the current menu page on a navigation bar (aside from the fact that I am a complete beginner :-) I have tried using a JQuery function that I found online, but none of them seem to work. Here is the function:

<script type="text/javascript">
    $(function(){
        var path=window.location.pathname;
        path=path.replace(/\/$/, "");
        path = decodeURIComponent(path);

        $('#menu a').each(function() {
            var href=$(this).attr('href');
            if (path.substring(0, href.length) === href) {
                $(this).closest('li').addClass('active');
            }

        });

    });
</script>

Here is my CSS:

@charset "UTF-8";
/* CSS Document */

#menu{ width:100%; height:40px; background-color: pink; border-bottom: 1px silver solid;}

#menu ul {

    list-style: none;
    margin:0 auto; 
    position: relative;
    padding:5px 0;
    width: 940px;
}

#menu ul li {
    display:inline;
    margin-left: 150px;

          }

#menu li:first-child {
  margin-left:0;
    }         

#menu ul li a {
  color: silver;
  display:block-inline;
  font: 16px "Comic Sans MS", cursive;
  text-align: center;
  text-decoration: none;  
        }


#menu ul li a:hover {
  background-color: yellow;
  text-decoration:underline;
}
.active{
    color:green;
    }

The HTML markup looks like this:

<?php
echo<<<END

    <div id="menu">
                    <ul>
                    <li><a id="home" style="color:white; font-size:16px;" href="$doc_root/index.php" title="Home Page">Enzo</a></li>
                    <li><a id="travel" href="$doc_root/travel/grid.php" title="My Trips"><span>travelling</span></a></li>
                    <li><a id="images" href="#">images</a></li>
                    <li><a id="words" href="#">words</a></li>
                    <li><a id="about" href="#">about</a></li>
                    </ul>
            </div>  
END;
?>

I am unsure what is causing the issue, it could be related to my CSS. I have simplified it to just include the color green for now, and I'm not certain if the "active" class is properly inserted into the CSS menu page. Any hints or alternative solutions would be greatly appreciated. Sending love to all! :-)

Answer №1

Make sure to verify that the $root_dir, which I assume is a PHP variable, follows this format:

<?=$root_dir?>

If you don't have short open PHP tags enabled, you can use:

<?php echo $root_dir; ?>

This ensures that it displays correctly in the HTML. If you inspect the page source and see $root_dir instead of the variable's value, that could be the cause of the issue.

Answer №2

It appears that there have been some adjustments made to your code

Updates in Javascript:

 $(function () {
            var path = window.location.pathname;
            path = path.replace(/\/$/, "");
            path = decodeURIComponent(path);

            $('#menu a').each(function () {
                var href = $(this).attr('href');
                if (href.indexOf(path) != -1) {
                    $(this).closest('li').find('a').addClass('active');
                }

            });

        });

Changes in CSS:

#menu ul li a {    
  display:block-inline;
  font: 16px "Comic Sans MS", cursive;
  text-align: center;
  text-decoration: none;  
        }

Adjusted HTML:

<div id="menu">
            <ul>
                <li><a id="home" href="http://localhost:50160/SelectedMenu.aspx" title="Home Page">Enzo</a></li>
                <li><a id="travel" href="#" title="My Trips"><span>travelling</span></a></li>
                <li><a id="images" href="#">images</a></li>
                <li><a id="words" href="#">words</a></li>
                <li><a id="about" href="#">about</a></li>
            </ul>
        </div>

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 deactivating the double class with jQuery

I need to implement a feature where all classes on a button are disabled if a specific class is present. I attempted to disable all classes, but it seems like I made an error somewhere. Jquery: if ($('.lgi_btn_cta_toggle').hasClass('lgi_ct ...

Customizing the jQuery Datepicker to only allow a predefined set of dates

I am looking for assistance regarding the jQuery datepicker. I have an array of dates and I need to disable all dates except for the ones in this specific array. Currently, the code I have does the opposite of what I need. I generate the array of dates in ...

Sending each step for evaluation

I am currently utilizing jQuery steps within a form wizard. I would like each step to display a different form every time it changes, along with previous and next buttons. Additionally, I am looking to submit the form via AJAX. However, when attempting to ...

Tips for preventing multiple clicks when posting AJAX requests in jQuery

Using Django, I was able to create a website and implement a voting page with jQuery AJAX. The code works perfectly fine as shown below: <!doctype html> <html> <head> <script src="jquery-1.10.2.min.js"></script> <met ...

Is it possible to showcase dates within input text boxes prior to POSTing the form?

I am currently working on a form that posts to a PHP script by selecting a date range. For a better understanding, you can check out my visual representation here. Before the data is posted, I would like to display the selected dates in empty boxes or inpu ...

Improving Vue Component on Navigation

I am facing an issue with my navbar where I have two versions. Version 1 features a white text color, while Version 2 has black text. The need for two versions arises due to some pages having a white background, hence the requirement for black text. Bot ...

Troubleshoot: Why Your jQuery AJAX Post Requests Are Failing

I'm having trouble understanding why this code isn't functioning properly. Currently, the php function "alias_valid" is just returning a string temporarily for testing purposes, so I've omitted including the php function here. The alert mess ...

generate a customized synopsis for users without storing any data in the database

In order to provide a summary of the user's choices without saving them to the database, I want to display it in a modal that has already been created. Despite finding some sources online, none of them have worked for me so far. Below is my HTML: &l ...

Retrieving data from the ASP.NET MVC server when selecting an item in jsTree

Utilizing jsTree in my ASP.NET MVC application, I have successfully implemented a tree structure to represent hierarchical data. Through a controller method that returns JSON data, I am able to access and display the tree as well as select nodes within it. ...

I'm having trouble changing the background color on my HTML Bootstrap website. Is there a way to easily add social network buttons as well?

I'm encountering a problem here. One of my friends assisted me with Bootstrap elements for my website, but after switching my site to Bootstrap, I am unable to change the background color on my website. Can someone lend a hand? Much appreciated! Also, ...

Developing a downloadable PDF version of an online platform

For weeks now, I have been tirelessly searching for a solution to a problem that has stumped me once before. The Challenge: My company created a sophisticated web-based data analytics suite for a major beverage distributor. Our client is now requesting a ...

"Discover the magic of creating a navigation menu with jQuery toggle - let me show

Recently, I managed to create a side navigation using the target function in CSS3. However, I am curious about how I can achieve the same result using jQuery without disrupting the layout. Any assistance would be greatly appreciated. Here is the code sni ...

PAYPAL PAYMENT Integration using REST API

I have integrated PayPal Client Side REST to my website. The sample code provided from the link below was used: https://developer.paypal.com/demo/checkout/#/pattern/client The code below was working for over a month, but today it is showing the following ...

Guide on how to gather values into a variable from the DOM using jQuery

Trying to make this function: The current issue I'm facing is that when changing a digit (by clicking on a hexagon), I want to save the selected number as the value of a hidden input field next to the digit in the DOM. Any ideas on how to achieve th ...

Having trouble loading the linked CSS file in the Jade template

My directory structure is organized as follows: --votingApp app.js node_modules public css mystyle.css views test.jade mixins.jade In the file mixins.jade, I have created some general purpose blocks like 'bo ...

Determine the Size of an Image File on Internet Explorer

Is there an alternative method? How can I retrieve file size without relying on ActiveX in JavaScript? I have implemented an image uploading feature with a maximum limit of 1 GB in my script. To determine the size of the uploaded image file using Java ...

Utilizing the .data() method for establishing a variable

I am facing an issue while trying to set a variable in a form using .data(). The problem is that I cannot see any output for the variable "fee" - it just appears blank. What I am trying to achieve is for the user to select a Type, enter a footage, and then ...

Unable to retrieve value - angularJS

An AngularJS application has been developed to dynamically display specific values in an HTML table. The table consists of six columns, where three (Work Name, Team Name, Place Name) are fixed statically, and the remaining three columns (Service One, Servi ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

How to align an image in the center of a circular flex container

I'm facing an issue in my Angular project where I have the following code snippet: onChange(event: any) { var reader = new FileReader(); reader.onload = (event: any) => { this.url = event.target.result; }; reader.readAsData ...