Change the color of child li to white when hovering over the parent li, and vice versa

Let me start off by explaining the issue I'm facing. Take a look at to get a better understanding of what I'm referring to. It needs to be a specific page, not the home page, so you can see the problem for yourself.

Now, when you hover over the menu, especially the one with a sub-menu, you'll notice that the child elements become "invisible" when the parent element is hovered over, as they are the same color as the background. The same happens when you hover over the child element.

What I'm trying to achieve is to have both elements change to white when the mouse is over either of them.

I have come up with this pseudo-code:

$('li.menu-parent-current > li.menu-child-current a').hover(
    function() {
        $(this).css('color', '#fdfdfd');
    }, function() {
        if (/* mouse leaves parent as well */) {
            // change child color to blue
        } else if (/* mouse did not leave parent */) {
            // make child color white
            $(this).css('color', '#fdfdfd');
        }
    });

I know there is definitely room for improvement in this code, so feel free to provide a better version if needed. :)

I've spent the past few hours trying to solve this issue, but I haven't been successful. I'm sure it's something simple, but I'm not very experienced with jQuery. Thank you in advance for any help you can offer. :)

EDIT:

Well... It turns out I was overcomplicating things. Sometimes being new to something doesn't allow for clear thinking. :P

Answer №1

It seems to me that using jquery is unnecessary in this case. Simple css does the job nicely:

li:hover{
    background-color: blue;
    color: white;
}

The child element li will take on the color scheme of the parent.

http://jsfiddle.net/7Jz9a/

Answer №2

To achieve this effect, consider relying solely on CSS rather than incorporating jQuery's ".hover()". Nevertheless, your current CSS contains conflicting styles for :hover that do not harmonize well with the duplicate jQuery .hover() functionality.

I suggest beginning by organizing the CSS code, and if necessary, resorting to jQuery to ensure a more streamlined approach.

Answer №3

To achieve this effect, you can utilize CSS in conjunction with jQuery.

If you opt for using only CSS to accomplish this:

li:hover a{color:#fff;}
li:hover ul li a{color:#fff;}

Alternatively, if you prefer to utilize jQuery for this task:

$('ul li').hover(
  function(){$(this).find('a').css('color','#fff');},
  function(){$(this).find('a').css('color','##36c');}
);

Answer №4

To implement

#menu li:hover a {
    color: white;
}

Answer №5

Experiment with this basic CSS tweak

#nav li:hover a{
  font-color: #FFFFFF;
}

Give it a go and see if it improves the design

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

Tracking changes in real time and calculating the sum with AJAX, PHP, and MySQL for efficient processing

Initially, I kindly request you to read this until the end. I am in dire need of assistance with my problem as I have searched for solutions but still remain clueless. https://i.sstatic.net/DAb1q.png Referring to the image provided, the first 'Produ ...

Is it possible for cluetip to function on a hyperlink located in the top right corner of a webpage?

Currently, I am utilizing jquery cluetip for my project. There is a link located at the top right-hand corner of the page, and upon clicking it, I expect the tooltip to display at the bottom of the click location. However, instead of appearing at the desir ...

Exploring the integration of AJAX and jQuery with Django 1.3

I am completely new to the Django framework, web development, and Python. Currently, I am trying to incorporate AJAX into my project but struggling to find a working sample. I need assistance with integrating AJAX or jQuery within a Django 1.3 project. Cu ...

Tips for avoiding accidentally selecting nearby text while holding down a button on your mobile device

My application requires the user to press and hold a button to record audio. However, on mobile devices, when the user holds the button, the browser attempts to select nearby text due to finger pressure. While this behavior is understandable, I want to pre ...

Creating a balanced height for child elements using CSS

If you are looking to display a list of colors with each color occupying an equal fraction of the height, here is how you can achieve it. Imagine presenting four colors in a list with a fixed height and a thick border around it: The example shown above is ...

The React task list updates the todo items on change, rather than on submission

As a newcomer to React, I have embarked on the classic journey of building a todo app to learn the ropes. Everything seems to be functioning smoothly except for one minor hiccup: When I input a new todo and hit "submit", it does get added to my array but d ...

Firefoxx unable to communicate with server through ajax requests

My dilemma involves transmitting data to my server using the json data type with ajax. Oddly enough, in Firefox the server fails to receive any data at all, while in Chrome and IE the data is successfully transmitted and displayed on the server console. H ...

Error in jQuery when element is not found

On my website, I use multiple jQuery functions, but not all of them are necessary on every page. These functions are all located in one XXX.js file, such as: jQuery(function() { $(".title").slug({ slug:'slug', hide: false }); }); ...

Tips for arranging Angular Material cards in columns instead of rows

I am currently developing a web application using Angular, and I've encountered an issue while trying to display cards in a vertical layout rather than horizontally. My goal is to have the cards fill up the first column (5-6 cards) before moving on to ...

Choose the initial full text that corresponds with a portion of the text

I am currently working on a page with multiple <string> tags containing different strings, like 'Is this a String?  Yes'. My goal is to use JQuery to locate the first instance of 'Is this a String?  ' and extract either ...

Tips for automatically refreshing a div in Drupal

I'm currently working with Drupal and I'm looking to update a specific field every X seconds without having to refresh the entire page. My current approach involves using the following code: <script> var refreshId = setInterval(function ...

Check the validity of the schema for an array in a

Hi everyone, I need some help with this JSON code I have. var menu = [{ "name": "Computers", "children": [{ "name": "Notebook", "children": [{ "name": "Apple" }, { "name": "Windows" }] }, { "name": "Tablets", "children": [{ "name": "Apple" }, { "name": ...

Django Implementation of JavaScript Confirmation Dialogue

Currently working on a Django form that requires a confirm/cancel dialog upon submission. I've considered sending POST data from jQuery, but I'm curious if it's possible to integrate a JavaScript dialog as middleware instead? ...

Is there a way to achieve horizontal alignment for the Twitter and Facebook buttons on my page?

By utilizing the html code provided, I successfully incorporated Twitter and Facebook "Like" buttons into my website. Initially, they were stacked vertically, which resulted in excessive use of vertical space. To optimize space usage, I decided to align th ...

Is it possible to save jQuery plugin initialization settings for future use?

Is it possible to save a default set of settings for a lightbox jQuery plugin, including options and callback functions, in a variable or array that can be referenced later in different contexts with varying configurations? For example, could I initially ...

Is it possible to restore the text to its original state?

Currently, I'm working on creating a simple website for server users to order items. However, I've encountered an issue related to the Navigator text. Here is how it currently appears: Upon inspecting the code below, you'll notice that the ...

How can you use JavaScript to create a JSON object using values from an HTML form textarea and then send

I need to create an HTML form that will send specific information in the Http-request body. { “info” : { “id” : “123” “text1” : <data from html text-box> } Therefore, my goal is to convert the provided data into a JavaScri ...

Spin only the outline with CSS

I have a unique idea for my webpage - three spinning circles surrounding text, while the text itself remains stationary. My challenge is to achieve this effect using only CSS, specifically by utilizing the .spinner-border class from Bootstrap. I cannot ma ...

Looking to reposition the control buttons above the slider in easySlider 1.7?

I am thoroughly enjoying the easySlider 1.7 and found it incredibly simple to implement. The only modification I wish to make is to relocate the next and previous buttons above the slider instead of below it. Does anyone have any suggestions on how to achi ...

I have two tables - one containing records and the other containing locations. I am interested in displaying the records from the first table without using a

I'm currently working on a project where I need to retrieve data from two tables: the admin table, which contains all the details, and the shop location table. First, I need to check the latitude and longitude of the shops using the shop_location tabl ...