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

Switching form field types with jquery

Hello there! I am currently attempting to use jQuery to change the type of <input> from submit to image so that an image is displayed instead of a button during the AJAX request. Here's what I have tried: $(document).ajaxStart(function() { ...

Unexpected behavior with CSS background-image transitions effects

Currently, I am experimenting with a hover swipe effect using div and background image. The background image utilizes the outer div's max-width and an inner div with a padding-bottom percentage to maintain the aspect ratio. However, there are several ...

What is the method for obtaining the current date when altering the system date to a previous time?

To ensure that my datepicker always displays the current date and does not allow selection of past dates, I need to update the date if the system date is in the past. If I change the system date to a past date, I don't want the datepicker to reflect t ...

Combining both inline and block positioning for div content

My goal with applying CSS styles is to create a visually appealing composition of content: https://i.sstatic.net/CpVXb.png After applying my styles, I received the following result: https://i.sstatic.net/Bfh5q.jpg li { position: relative; list-styl ...

Store Form Input as JSON Data File

Seeking advice on the optimal method to save submitted form data to a separate file called data.json, for future reference. The form layout is quite basic: <form> <fieldset> <label for="name">Name:</label> &l ...

Master br tag in HTML: a complete guide

I am currently struggling with understanding how to correctly use the line break tag in HTML. During a recent class test, we were tasked with identifying issues within an HTML script, and I mistakenly believed that < /br> was incorrect. I am unsure ...

Angular displays X items in each row and column

I've been struggling with this task for the past 2 hours. My goal is to display a set of buttons on the screen, but I'm facing some challenges. The current layout of the buttons doesn't look quite right as they appear cluttered and unevenly ...

Hexagonal Shape with Rotating Border Gradient in CSS

Looking to create a hexagon shape with a rotating gradient border. Here's an example GIF for reference: https://i.stack.imgur.com/Ah1AO.gif I attempted using CSS only but the use of :after and :before tags proved limiting since they rely on border s ...

The anchor tag is failing to register when placed inside a dynamically created table cell

I'm attempting to place an anchor tag within a cell of an HTML table. Here is the code I am using, but for some reason, the tag is not being identified and no hyperlink is displayed in that cell. Is there an issue with the syntax? $("#tranTbodyI ...

What exactly is HTML cloud storage all about?

As I work on developing an app through phonegap, one question that comes to mind is the possibility of storing information online. For instance, if there's a number variable that increases when a button is pressed, can this value be saved somewhere an ...

What are some ways to obscure the stored password in a database with characters like asterisks or other symbols

Currently, I am working on developing the admin features for my website and have been using this resource to assist with adding users: One issue that I have encountered is that when I added a password field, it appears in its stored form which is not idea ...

Center the image and align the floated texts on each side horizontally

I've recently started learning about HTML and CSS, but I'm having trouble grasping one concept. My goal is to align two floating <p> elements on either side of a centered <img>. Here's an example of what I'm trying to achiev ...

`Changing the output of a jQuery .load function`

I've created a webpage that pulls content from an article, but I'm looking to display only the first 100 words of the article. Currently, my page successfully loads the article using this code: $(function(){ $('#startOfArticle').l ...

How can you Use CSS to Float Elements Left and Right while adjusting their Width Dynam

Is it possible to have two columns side by side with dynamic text using only CSS? I'm trying to make the left main text "break" into a new line when it reaches the right category, which is also dynamic. Do I need to use JavaScript for this? ! Link ...

Mastering Jquery Isotope integration with Wordpress for seamless filtering

I've been struggling to implement isotope filtering in my project. As a beginner in jQuery/javascript, I wanted to integrate isotope functionality into a client's website. Despite researching extensively on Stack Overflow and various websites, I ...

Masonry ajax loading is not functioning properly

My images are supposed to load via ajax, but I am experiencing some issues. Sometimes it works perfectly fine, but most of the time they overlap each other like in this example. Where did I go wrong? Once I figure this out, I'll be able to refactor my ...

Exploring the Link Between jQuery and HTML

Is it possible to connect between an HTML file and a jQuery file? Here is an example scenario: jquery-test.js: $(document).ready(function(){ $('#inputForm').submit(function(){ $('#inputForm :text:not("#submit")').each(func ...

Unable to delete data in Laravel 8

I am new to Laravel and facing an issue when trying to delete a row with a modal. The problem is that only the first row is getting removed and I can't figure out the reason. Here is my modal setup: <p class="card-text"><small clas ...

Snippets of the webpage peeking through before the Fakeloader takes over

After implementing fakeloader to preload my content here, I have noticed that my site header often appears before the fakeloader preload animation completes. Is there a way to delay showing the content until the fakeloader is finished loading? Here is the ...

How do I disable scrolling while the animation is running in CSS?

Is there a way to disable scrolling while animating in CSS without using overflow:hidden on the body tag? ...