What could be causing the color property to remain unchanged on my <li> elements when using the .hover() event handler?

<!doctype HTML>
<html>

<head>
    <title>HUSTLE PAY$</title>
    <meta name="viewport" content="width = device - width, initial scale=1.0">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Looking to implement some jQuery effects:

  1. I want to change the background color and font color of an LI element on hover. Although the background color changes successfully, I'm struggling with changing the font color to white.

  2. The original list colors should return once the hover effect is removed.

    $(document).ready(function(){
        $('li').hover(function(){
            $(this).css({"background-color" : "#0066cc" , "color" :   "white"})
        },function(){
            $(this).css({"background-color" : "#0000cc" , "color" :  "black"})
        });
    
    })
    

My current jQuery script doesn't seem to work properly. Any ideas on what needs to be changed to make it function correctly?

    .menu{
        display:inline;
        float:left;
        width:10%;
    }

    ul{
        background-color:#0000cc;
        display:inline;
        float:left;
        border:1px solid black;
        width:60%;
        height:100px;
        text-align:center;
    }
    li{
        border:1px solid red;
        list-style-type:none;
        padding-left:0px;
        padding-right:0px;
        height:30px;
        font-size:30px;
        text-align:center;
    }
    a{
        color:black;
    }

</style>
</head>

   <body>
      <header>
        <div>
            <img src = "greenProject/images/menu.jpeg" class = "menu" alt =  "Image not available">
        </div>

        <ul>
            <li><a href = "#">Home</a></li>
            <li><a href = "#">About Us</a></li>
            <li><a href = "#">Contact Us</a></li>
        </ul>

</header>
</body>
</html>

Answer №1

The color of the text in the list item changes as expected, however, this does not impact the text color of the links within the list items.

In order to change the text color of the links, you must target them specifically inside the list items:

$(document).ready(function(){
  $('li').hover(function(){
    $(this).css({"background-color" : "#0066cc" }).find('a').css({ "color" : "white" });
  },function(){
    $(this).css({"background-color" : "#0000cc" }).find('a').css({ "color" : "black" });
  });
});

This can also be achieved using CSS without relying on a Javascript event:

li:hover {
  background: #0066cc;
}
li:hover a {
  color: white;
}

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

Personalized Angular dropdown menu

Recently, I've started delving into angularJS and I'm eager to create dropdowns and tabs using both bootstrap and angular. Although there is a comprehensive angular bootstrap library available, I prefer not to use it in order to gain a deeper und ...

Combining arrays using value comparison in Google Analytics and MongoDB

Help me out, Stack! This job is driving me crazy. Here's what I'm working on: Using the Google Analytics NodeJS SDK, I'm retrieving data about the most visited pages of my website. By leveraging Google's user-friendly URLs (slugs), I se ...

Ways to resolve the issue of an invalid hexlify value error in ethers.js

Error: hexadecimal value is not valid (argument="value", value="PRIVATE_KEY", code=INVALID_ARGUMENT, version=bytes/5.7.0) During the development process of an application that transfers a token from one wallet to another using Ethers.j ...

Internet Explorer causing problems with JQuery

I encountered an error with the following code snippet: jQuery.post('/user/result/generate',{ 'id': getHidden() }, function(html) { $('#result').html(html); }); The error message is: ...

Is there a way to compare a particular JSON value to the values in an array and determine if they are similar? If so, how can I update the JSON value with the array value

My inventory of wholesalers looks like this: const wholesalers = [ "B536 - T QUALITY LTD", ]; I also have a collection of JSON objects as shown below: { 'Preferred Wholesaler': 'T-Quality' }, { 'Preferred Wholesal ...

Refresh Next.js on Save

My current setup involves using Next and React for my application. I've noticed that the application only reloads when I manually run next build and restart the server. Is there a way to achieve automatic reloading without having to go through the pro ...

Unable to retrieve Object property using computed method in Vue 3 vuex results in undefined value

When attempting to display the values of a computed property in the template using the syntax {{todo[0]['title']}}, I am receiving an 'undefined' output. However, using {{todo[0]]}} allows me to render the entire object. My goal is to b ...

What could be causing Highchart to return the error message "Typeerror: undefined variable byte"?

My current project involves creating a graph using highchart and updating values every second. I am also working on displaying data in Mb, Kb, and Gb format on the graph by developing a function to convert byte values. Here is a snippet of my code: // hi ...

Displaying hyperlinks within a table that is contained within a div on the current webpage

I have created an HTML page that looks like this: <!DOCTYPE html> <html> <body bgcolor="#EBF5FB"> <h1 align="center">SLA Monitor Reports </h1> <div id="link" align="center"> <table cellpadding="25px" bord ...

Wordpress API returned an error with the code "rest_invalid_json" and the message "Invalid JSON data provided."

I'm struggling to figure out where I went wrong in my code. Could someone please offer some insight into what might be causing the error I'm encountering? Below is the code snippet I'm working with. I can confirm that the route is being succ ...

Is it possible to assign a class to the parent element when a Bootstrap 4 dropdown menu is expanded?

Is there a way to include a class in the parent element when a Bootstrap Dropdown is open? Here is the HTML code: <div class="item-content"> <div class="block-handle"> <div class="dropdown"> <a cl ...

span tag used within a Twitter Bootstrap 2 modal

Using span structure in modal view is causing overflow issues when using .row, .row-fluid, or spans within the modal. <div id="login_register_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&g ...

Adding an image to a cell within a PUG table

I am currently utilizing PUG to display the results of an API call. Among the properties returned is a link to an image, which I would like to showcase in the final column. Ideally, I want to ensure that the image size is responsive and doesn't overwh ...

How come repeating a call to an animation function in three js results in skipping certain animations?

I am trying to create a repeated animation sequence involving a sphere moving and then a rectangle, followed by displaying the number of times it has been repeated in a div called expected_output. However, I'm encountering an issue where the animation ...

Instructions for deactivating a drop-down list using radio buttons in JavaScript

I am working on a project where I have 3 radio buttons and 3 different combo boxes in my HTML. I want to use JavaScript to enable/disable the combo boxes based on which radio button is selected. For example, when the G1 radio button is clicked, I want on ...

CSS posing a variety of issues: opacity affecting text elements as well as misplacement of divs

<!DOCTYPE html> <html> <head> <title>Title</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <style type="text/css"> body { backgr ...

Error encountered when attempting to upload a canvas base64 image to parse.com with a broken image link

My application consists only of HTML and JavaScript. Take a look at my code snippet below for saving the base64 imgData on Parse.com: var parseFile = new Parse.File("mypic.jpeg", {base64:imgData}); parseFile.save().then(function(data) { console.log(" ...

Validate that a string is a correct file name and path in Angular without using regular expressions

Currently, I am using regex to determine if a string is a valid file name and path. However, when the string length becomes longer, it ends up consuming a significant amount of CPU, resulting in browser performance issues. public static readonly INVALID_F ...

Executing actions based on events in PHP

Is there a way in PHP to trigger event Y when action X is performed? Currently, I am using jQuery Ajax at regular intervals to track the latest updates, but I believe this method puts unnecessary load on the server. Here's what I'm trying to acc ...

Implementing sorting functionality in a list with Vue.js by incorporating an arrow icon in the column using v-bind

Is there a way to implement sorting functionality in Vue.js by clicking on table headers and displaying an arrow to indicate the sorting order? I am struggling with creating a function in Vue for sorting and using v-bind to add the arrow. Can someone guide ...