Using jQuery to implement interactive hover effects on individual items within a list

I'm currently developing a project that involves displaying speech bubbles next to each list item when hovered over. These speech bubbles contain relevant information specific to the item being hovered on. To achieve this functionality, I've created 6 different .move (.move1, .move2, .move3. etc) classes with distinct position settings for each speech bubble scenario. You can view a demo of what I'm working on in this jsfiddle link: https://jsfiddle.net/sLemf2z0/1/

Currently, I am attempting to implement a loop in Javascript or jQuery that assigns the appropriate .move class to the corresponding list item being hovered over. However, my current implementation is not functioning as expected. How can I ensure that the correct .move classes are applied based on the list item being hovered over using a loop?

Below is the HTML, CSS, and JavaScript code snippet outside of the jsfiddle environment:

HTML

<ul class="grid1Ul">
   <li>&nbsp;Arthritis
      <ul class="clearfix ">
         <li class="subLi">Arthritis is not good for you.</li>   
      </ul>
   </li>
   <li>Back &amp; Neck Pain
      <ul class="clearfix ">
         <li class="subLi">Neck Pain is not awesome at all</li>   
      </ul>
   </li>
   <li>&nbsp;&nbsp;&nbsp;Muscle Strains/Sprains
      <ul class="clearfix ">
         <li class="subLi">Muscle strins is not awesome at all</li>   
      </ul>
   </li>
   <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sport/Auto/Work Injuries
      <ul class="clearfix">
         <li class="subLi">Sports and Auto injuries are definitely not awesome</li>   
      </ul>
   </li>
   <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Orthopedic Post Surgical
      <ul class="clearfix">
         <li class="subLi ">Surgery is not very awesome either</li>   
      </ul>
   </li>
   <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gait and Balance Training
      <ul class="clearfix">
         <li class="subLi">gait and balance training is awesome!!!!!!</li>   
      </ul>
   </li>
</ul>

CSS

.grid1Ul {
    color: #982304;
    font: italic 700 35px / 48px Arial;
    text-shadow: 2px 4px 5px rgba(0, 0, 0, 0.32);
    list-style-type: none;
    position: absolute;
    top: 391px;
    left: 121px;
    line-height: 1.7em;
    z-index: 3;
}

.gridUl li {
   position: relative;
}

.subLi {
   list-style-type: none;   
}

/* Rest of the CSS styles remain unchanged */

Javascript/Jquery

 for(i = 1; i < 7; i++) {

        var numString = "" + i;

       $(".grid1Ul li:nth-of-type(" + numString + ")").hover( function() {
           $(this).children().toggleClass("move" + numString);   
       });

    }

Answer №1

The jQuery method .hover() requires two arguments: handlerIn and handlerOut (mouseenter and mouseleave).

If you are already using jQuery, it's recommended to use .each() instead of a traditional for() loop.

Here is an example:

// The each() function provides access to an index
$('.grid1Ul > li').each(function(index) {
    $(this).hover(function() {
        // Adjust the index by one since it is 0-based
        $(this).children().addClass("move" + (index + 1));
    }, function() {
        $(this).children().removeClass("move" + (index + 1));
    });
});

Check out this JSFiddle for a live example

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

Optimizing Bootstrap Carousel Size

How can I create a carousel using Bootstrap without it taking up the entire window and hiding other div elements? Any suggestions on how to fix this issue would be greatly appreciated. I found this example during my research, but I'm still struggling ...

Please only choose the immediate children of the body element

<body> <div> <div class='container'></div> </div> <div class='container'></div> </body> Is there a way to use jQuery to specifically target the second container div dire ...

The depth order of overlapping elements and the rotation effect achieved through

I have created some interactive flip cards using CSS and HTML with transitions Currently, when you hover over a card, it flips and enlarges. However, I am facing an issue with positioning the hovered card on top using z-index and relative position due to ...

Utilize ASP.Net to Retrieve and Showcase RSS Feeds

Looking to read and display a specific feed on my website. Developed in C# using .NET 2. Attempted to follow this tutorial: Encountered the following error: A column named 'link' already belongs to this DataTable: cannot set a nested table n ...

Issues with Angular radio buttons are causing them to not be selected properly

When it comes to radio buttons, they should be checked based on certain conditions. <input data-ng-checked="user.contract1 || user.contract2" data-ng-model="user.agreed" type="radio" data-ng-value="true"> Yes <input data-ng-checked="!user.contrac ...

Tips for simultaneously displaying and updating HTML content

I have a hidden bootstrap alert div that is meant to display errors. I am trying to simultaneously show the alert and change its HTML content using jQuery. I attempted the following code snippet, but it does not seem to be working as intended. var erro ...

How to target a class with a specific number in CSS selectors

My mobile hybrid application is built with Sencha Touch 2 and requires some customization based on the iOS version it's running on. In my Sass stylesheet, I originally had the following selector: .x-ios-7 { /* add iOS7 customizations here */ } How ...

What is the best way to retrieve a particular value from a database using PHP?

$sql="Select chanceNo From gm_gacha_token1 where token_code='$mytoken'"; $result=mysql_query($sql); $count=mysql_num_rows($result); Language: PHP I am attempting to retrieve a single value of `chanceNo. Is there a way to get this value and stor ...

Create a custom VueJS component by utilizing an npm package

Looking to navigate around X-frame restrictions? You can check out this npm package: https://www.npmjs.com/package/x-frame-bypass To make it work, include the following tag within your HTML: <iframe is="x-frame-bypass" src="https://example.org/">& ...

Troubleshooting AJAX Problems in ASP.NET and C#

I am currently working on implementing a WebMethod call in my C# code behind using AJAX. I have a Bootstrap Modal that should be displayed when a linkbutton is clicked, triggering the execution of the WebMethod through AJAX to populate a table in the modal ...

Discover the Magic of Angular 8 and Bootstrap 4 Tooltips

I'm trying to implement tooltips from Bootstrap 4 into my web application. According to Bootstrap documentation, I should initialize the tooltips with the following code: $(function () { $('[data-toggle="tooltip"]').tooltip() }) (Implement ...

Cross-Origin Resource Sharing problem encountered with the webservice thumbnail.ws

I am attempting to create an HTML page that generates a snapshot of a URL using the free webservice provided by thumbnail.ws. Below is my code snippet: var myurl = "http://api.thumbnail.ws/api/API_KEY/thumbnail/get?url=http://maps.google.com/?q=36.82 ...

Slider malfunctioning following AJAX loading

After the user clicks on #mail-wrap, I am attempting to trigger the ready event which loads another page with AJAX so that sss() can be refired. Despite my efforts, it seems like it is not working as expected. Can you help me identify what might be going w ...

Methods that are static and defined within the Promise constructor

While examining the static methods of the Promise constructor, I noticed that there are two methods called resolve and reject: console.log(Object.getOwnPropertyNames(Promise)) // Array(7) [ "all", "race", "reject", "resolve", "prototype", "length", "name" ...

The proper method for developing Vue components that are dependent on the parent build tools

In my experience, this issue might also arise in other frameworks with plugin/component ecosystems that rely on specific build tools (such as React components with JSX). While Vue serves as my use-case. Having developed numerous Vue components as single . ...

How to create a Vue.js animated navbar with scroll opacity

I am looking to create a fixed navbar that changes opacity based on user scrolling behavior. Initially, I want the navbar background to be transparent and then transition to white as the user scrolls down the page. An example of what I am trying to achieve ...

Organize the table data based on time

My website specializes in offering cell phone rental services. Users can visit the site to view the available devices that we have. I designed the display of these devices using a table format and components from "@mui/material". One of the columns in thi ...

The process of loading an image becomes stuck once the submit button is pressed

I have multiple JSP pages where I use an ajax call to submit data. On these pages, I am able to display a loading image before the ajax call and hide it once the call is complete, which works perfectly. $(document).ajaxComplete(function() { $("#loadi ...

Ways to showcase the chosen choices from earlier stages in the intelligent guide

Currently, I am developing a user interface for my latest project and have incorporated the Smart Wizard from . In particular, I am utilizing the smartwizard-modal.html found in the examples section of smartwizard. This specific smart wizard modal consis ...

Accessing a JSON file from a nearby location using JavaScript

I am currently working on an artistic project based on weather data, which will be hosted locally with the JSON file updating via FTP synchronization. This means that the JSON file will be sourced from the same computer where it is stored. The code snippet ...