"Implementing JQuery addClass Functionality to Enhance Menu Sty

I've created a menu that is stacked on top, with the "Representaciones" section shown on the same page below a welcome image. However, when I click on it, everything works fine but if I refresh the page, the "selected" class disappears from "representaciones" and shows up on "home". I'm struggling to make it work consistently, any help would be appreciated.

<div id="menu">
    <ul>
        <li>
        <a href="index.php" class="selected">HOME</a>
        </li>
       <li>
       <a href="#representaciones">REPRESENTACIONES</a>
       </li>
       <li>
       <a href="productos.html" target="_parent">PRODUCTOS</a>
       </li>
       <li>
       <a href="eventos.html" target="_parent">EVENTOS</a>
       </li>
       <li>
       <a href="contacto.php" target="_parent">CONTACTO</a>
       </li>
   </ul>
</div>

css

#menu{
padding-left:225px;
margin: 0 auto;
position:absolute;  
width:900px;
height:142px;
background-color:#f6f6f6;
}

#menu ul li a {
    text-align:center;
    float:left;
    list-style: none outside none;
    font-weight:200;
    color: #919ca1;
    font-size:20px;
    text-decoration:none;
    padding-top:60px;
}

#menu ul li a:hover{
    color: #f6f6f6;
    background-color:#d3242c;
    padding: 61px 40px 61px 40px;
}

#menu ul li a.selected{
    color: #f6f6f6;
    background-color:#d3242c;
    padding: 61px 40px 61px 40px;
}

jquery code snippet:

$(document).ready(function () {
    $('#menu li a').click(function () {
        $('#Menu ul li a').removeClass('selected');
        $(this).addClass('selected');
    });
});

Answer №1

It is my understanding that this is the basic functionality of web browsers. When you refresh a page, it typically returns to its original state.

To maintain a specific state for your page, you may consider storing it in either a cookie or local storage. This way, you can retrieve the stored state after the page reloads and reselect the desired element accordingly.

Answer №2

To make the correction, you will need to update ctMenu to menu

$(document).ready(function () {
    $('#menu li a').click(function () {
        $('#menu li a').removeClass('selected');
        $(this).addClass('selected');
    });
});

FIDDLE DEMO

Answer №3

Here is a helpful suggestion

$(document).ready(function () {
    $('#menu li a').click(function () {
        $('#menu li a').removeClass('selected');
        $(this).addClass('selected');
    });
});

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

Generating dynamic ID attribute through JavaScript

Here is a portion of my recursively rendered partial view: <ul> @if (item != null) { <li id="@item.Id"> @item.ActionName @if (item.CMSModels.Count > 0) { @Html.Partial("Ch ...

Pattern to locate a CSS class identifier

I've been working on creating a regex to match valid CSS class name structures. Currently, my progress looks like this: $pattern = "([A-Za-z]*\.[A-Za-z]+\s*{)"; $regex = preg_match_all($pattern, $html, $matches); However, there are certai ...

When the *ngFor directive disrupts the CSS Grid Layout, resulting in all items being displayed in a single column

I am a beginner in the world of programming and web development. Currently, I am working on building my own personal website. My goal is to arrange boxes in a grid with 4 columns, similar to the layout you can find at this link: Each box represents an ob ...

Tips for uploading information to a web service

Issue Description:- I am attempting to access web services from a different domain (i.e. Systems are not locally connected) and encountering the following error: "Failed to load : Response for preflight is invalid (redirect)" var username = "<a href= ...

The Safari 7.1+ browser seems to be having trouble with the spotlight effect

Why is the CodePen not functioning properly in Safari? Despite working well in Chrome and Firefox, it completely fails to work in Safari 7.1+. Can anyone provide some insights? http://codepen.io/cchambers/pen/Dyldj $(document).on("mousemove", function( ...

Is it possible to customize the color of an SVG image within Next.js using next/image?

import Image from 'next/image' ... <Image src="/emotion.svg" alt="emtion" width={50} height={50} /> I am trying to change the color of the SVG using next/image. However, applying the following CSS rule does not seem ...

Unusual characteristics of decision-making

Here is a snippet of my JavaScript code: function getSelectedText(){ if(window.getSelection){ select = window.getSelection().getRangeAt(0); var st_span = select.startContainer.parentNode.getAttribute("id").split("_") ...

Utilizing jQuery's .ready() method within the body section of a document to modify the onload event following the printing of the body

Recently, I took over a tabbed page that uses multiple queries to determine which tabs should be displayed. My goal is to add some PHP logic that will automatically set focus on a specific tab when the page finishes loading. I'm wondering if it&apos ...

What is the method for utilizing the onmouseover function to emphasize a specific part of an SVG graphic?

I am attempting to create an interactive SVG map of Europe. Each country is represented by a path element, and I want the opacity of a country to change to 0.5 when it is hovered over. Despite trying to define a JavaScript function for this purpose, noth ...

Switching "this" keyword from jQuery to TypeScript

A piece of code is functioning properly for me. Now, I aim to incorporate this code into a TypeScript application. While I prefer to continue using jQuery, I am unsure how to adjust the use of this to meet TypeScript requirements. if ($(this).width() < ...

The relationship between the size attribute in HTML and CSS styling based on element size

On one of my pages, there is an input field that looks like this: <input type="text" size="20" name="whatever" /> I would like to keep the size attribute for the input field instead of using CSS. Now, I want to add a select box to the same form wit ...

Replace the typical bootstrap text class with stylish and modern google material icons

As a newcomer to the world of javascript, I acknowledge that my approach may not be ideal... I'm attempting to modify the color of a material icon upon clicking it. Essentially, I want to toggle it on and off. The challenge lies in the code's in ...

The process involves transferring information from a specific div element to a database. This particular div element obtains its data after receiving an appended ID

It's a bit complicated, but I'm working on creating a tag system. I'm fetching data from a database with an AJAX call using the "@" symbol. Everything is working fine - the tags are being generated, but I'm having trouble retrieving and ...

Problems arose when attempting to adjust the size of the container function

I've been working on a Joomla 2.5 template that consists of three main sections: top, container, and footer. While trying to set the "container" section to have a minimum height of 100%, I faced various challenges which led me to use jQuery for assist ...

How can I fix the issue of not being able to scroll up on a page in Chrome using jQuery

When at the bottom of the page and clicking on 'Send us your resume', the intent is for the browser to scroll to the top of the page. This functionality works fine in IE and Firefox, unfortunately, it does not behave as expected in Chrome. Here& ...

Looking for guidance on utilizing pushState and handling onpopstate events?

By using ajax, I am able to load specific page content without refreshing the entire page. In order to make the back button functionality work, I utilize pushState and onpopstate as shown below: function get_page(args){ .... $.ajax({ url ...

Tips for extracting data from various select drop-down menus within a single webpage

As a newcomer to JQuery, I apologize if this question seems basic. I have a page with 20 categories, each offering a selection of products in a drop-down menu. The user will choose a product from each category, triggering an ajax call to retrieve the pric ...

enabling the editing of two input fields by clicking on a button

Looking to make two input fields editable with a button click. Once edited, the data from these fields will be sent to separate columns in a database using ajax. Struggling to find a solution through online search. Any advice is appreciated! ...

Is there a way to delete a text link without deleting the entire class?

I am looking to specifically remove the link that contains the text "Skin" within the h3 tag. The challenge I am facing is that when I use the code $("h3:contains('Skin')").remove(); it removes the entire h3 element, whereas I only want to remove ...

If the session is not set, direct to the login page after an Ajax request

In order to ensure that the $_SESSION is set on each page, I have included a 'check session' page. This means that the 'check session' script is also included on pages with AJAX/php functionality. For example: <?php require_once gl ...