Choosing a menu option with jQuery

Can someone help me with making a menu option selected in HTML? Here is my code:

<ul class="ca-menu">
                <li>
                    <a href="#">
                        <span class="ca-icon"><img src="images/home.png"></span>
                        <div class="ca-content">
                            <h2 class="ca-main">Home</h2>
                            <h3 class="ca-sub">Home</h3>
                        </div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <span class="ca-icon"><img src="images/about.png"></span>
                        <div class="ca-content">
                            <h2 class="ca-main">About</h2>
                            <h3 class="ca-sub">About</h3>
                        </div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <span class="ca-icon"><img src="images/photo.png"></span>
                        <div class="ca-content">
                            <h2 class="ca-main">contact</h2>
                            <h3 class="ca-sub">Contact/h3>
                        </div>
                    </a>
                </li>

            </ul>

Here is the corresponding CSS:

.ca-menu li:hover{
background-color: #000;
}
.ca-menu li:hover .ca-icon{
color: #ff2020;
-webkit-animation: moveFromBottom 300ms ease;
-moz-animation: moveFromBottom 300ms ease;
-ms-animation: moveFromBottom 300ms ease;
 }
.ca-menu li:hover .ca-main{
 color: #000;
-webkit-animation: smallToBig 300ms ease;
-moz-animation: smallToBig 300ms ease;

-ms-animation: smallToBig 300ms ease;
}
.ca-menu li:hover .ca-sub{
color: #000;
background-color: #ff2020;
-webkit-animation: moveFromBottom 500ms ease;
-moz-animation: moveFromBottom 500ms ease;
-ms-animation: moveFromBottom 500ms ease;
}

$(document).ready(function(){
    $("li a").click(function(event){
        $('#navigation li').removeClass();
        $(this).parent().addClass('active');
        event.preventDefault();
    });
});

I need to add an active class for when the li element is clicked. I don't want to change the hover effect. Can anyone assist me with this?

Answer №1

The question seems a bit unclear. If you're asking how to differentiate the active page's menu item from others in the menu:

One way to achieve this is by adding a specific class to the html or body tag and then styling the menu items accordingly. For example:

<body class="section-home">
...
<ul class="ca-menu">
    <li class="menu-home"> ....

This way, the home page would have the class section-home, while other pages like 'about' could have section-about.

Your CSS could look something like this:

.section-home .menu-home,
.section-about menu-about,
...
{ 
    <active styling>
}

Although this method works effectively for simple websites (I've used it for a long time), it might become cumbersome if your CMS generates menu items dynamically.

Best of luck with implementing this!

Another perspective

If you meant how to style a page on hover, consider using CSS instead of jQuery. This approach can provide similar effects without the need for JavaScript.

Also, think about browser compatibility:

  • Are you targeting IE9+ as well as modern versions of Chrome and Firefox?
  • Remember that mobile browsers don't support hover effects - will your site still function correctly?

There are many factors to consider, so good luck with your design decisions.

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

Using Ajax.BeginForm with BeforeSend functionality

My MVC website has multiple Ajax.BeginForm elements, and I am looking to handle the beforeSend event of my Ajax calls. While the code below works for manual jquery ajax calls, it does not seem to work with the Ajax.BeginForm helpers: $.ajaxSetup({ &a ...

Restricting choices once user selects an option (HTML)

Currently, I am faced with a dilemma regarding two sets of options for an HTML form. One set consists of various units (such as cm, in, ft.), while the other set includes different locations. The selection of a unit would restrict certain location option ...

The built-in browser form validation is failing to function properly within a Vuetify v-form component

I want to utilize the default form validation of browsers for basic validations like required and email, while handling more complex validations in my service layer. However, I am having trouble implementing the required and email properties in my form. ...

Series of responses cascading from one another

Users are experiencing an issue where the need for adjusting margins based on the depth of responses in posts. Currently, this involves setting a margin-left value of 40px for one level deep reactions and 80px for two levels deep, and so on. To address th ...

employing rowspan functionality within a JavaScript application

How can I achieve a table layout where the first two columns are fixed for only one row and the remaining rows are repeated 7 times? Additionally, is there a way to make the bottom border of the last row thicker? Check out the expected table for reference. ...

`The ng-binding directive seems to be malfunctioning while ng-model is functioning properly

Currently, I am in the process of learning how to utilize Angular (1.3.10). My objective is to create two input fields that will specify the suit and value for a hand of playing cards. In my attempts to achieve this, I have encountered an issue. When I man ...

Failure to highlight items when using the multiple select function

After adding a select all button to a multiple select, I encountered an issue. Although all the items are being selected, they are not highlighted when clicking on the select all button. Below is the code snippet: <div class="label_hd">Profiles* {{ ...

import jQuery into a JavaScript file

I'm currently working on a Chrome extension that relies on background.js to perform basic tasks. I need to include jquery.js in my background.js file so that I can utilize its ajax function, but I'm unsure of how to achieve this. Is it even possi ...

What is preventing jQuery UI from detecting jQuery?

I have successfully created a JavaScript widget that is capable of being embedded on any third-party website, in any environment. The widget relies on jQuery and jQuery UI for its functionality. After following the guidelines outlined in How to embed Javas ...

Utilizing Angular's ngShow and ngHide directives to hide spinner when no data is retrieved

I'm having an issue with the HTML code below. It currently displays a spinner until a list of tags is loaded for an application. However, the spinner continues even if no events exist. I want to make the spinner disappear and show either a blank inpu ...

Reiterating the text and determining the length of the string

Currently, the script is capable of calculating the width of the script based on user input and can also determine the width of a string. However, I am facing an issue when attempting to repeat a string entered by the user. For example, if the user input ...

Is the JSON returned by WCF Data Services incorrect?

Currently, I am in the process of updating a software application that previously utilized jQuery 1.3.2 to interact with a WCF Data Service (also known as ADO.NET Data Services or Astoria) to now work with the most recent version of jQuery (1.4.2). Unfortu ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Executing a Python script within a Django project by clicking on an HTML button

There's a Python script file located in a Django project, but it's in a different folder (let's call it otherPythons). I'm looking to execute this Python file when an HTML button is clicked using JavaScript. Only looking for solutions ...

What are the reasons behind the jQuery file upload failing to work after the initial upload?

I am currently utilizing the jQuery File Upload plugin. To achieve this, I have hidden the file input and set it to activate upon clicking a separate button. You can view an example of this setup on this fiddle. Here is the HTML code snippet: <div> ...

leaflet.js - Place a marker when clicked and update its position while dragging

Currently working on a small project that requires the ability to place a marker on an image-map powered by leaflet.js, and update its position if it's dragged. I was using the code below for this functionality but keep getting an error 'marker n ...

Problem with stellar.js, boostrapv4, and jQuery 3.2.1 (lack of jQuery migrate support)

Having some issues integrating stellar with Bootstrap v4 and jQuery 3.2.1 as I keep encountering the error message "Uncaught TypeError: elem.getClientRects is not a function" Check out this JSFiddle link $.stellar({ horizontalScrolling: false, vertic ...

Tips for locating elements based on the final portion of their identifier using jQuery

When using jQuery, I need to locate an element based on its id. The original id is: txtHistoricalPricesDate In a typical situation, I would use the following code to find it easily: var element = $('#txtHistoricalPricesDate'); However, due to ...

Tips on slowing down the Jquery UIBlock Plugin

Currently, I am implementing a plugin found at http://jquery.malsup.com/block/#overview. However, I am interested in configuring the blockUI feature to only be displayed if an AJAX request takes longer than 1 second. Otherwise, I would prefer for nothing ...

Button located beneath or above each individual image within the *ngFor loop

With the *ngFor loop, I am able to populate images but now I want to include a button below or on each image. Unfortunately, my CSS knowledge is limited. Below is the code I have: HTML Code <div class="container"> <div ...