reveal or conceal content on hover of mouse pointer

I am currently working on a section that functions like jQuery tabs, but instead of requiring a click to activate, I want it to work on mouse hover. The current implementation is somewhat buggy - when hovering over a specific section, it displays as expected, but if the cursor moves and then hovers over the same area again, it stops working. How can this issue be resolved without making changes to the HTML structure?

<div class="section-option">
  <ul>
    <li>
      <div class="color-option-1 active"> </div>
      <p>JBL Clip2</p>
    </li>
    <li>
      <div class="color-option-2"> </div>
      <p>Bluetooth</p>
    </li>
    <li>
      <div class="color-option-3"> </div>
      <p style="word-wrap: unset !important;">Wasserdicht</p>
    </li>
    <li>
      <div class="color-option-4"> </div>
      <p>Akku</p>
    </li>
    <li>
      <div class="color-option-5"> </div>
      <p>Audiokable</p>
    </li>
  </ul>
</div>
<div id="color-option-1" class="color-option">
  <p>content 1 </p>
</div>
<div id="color-option-2" class="color-option hide">
  <p>content 2 </p>
</div>
<div id="color-option-3" class="color-option hide">
  <p>content 3 </p>
</div>
<div id="color-option-4" class="color-option hide">
  <p>content 4 </p>
</div>
<div id="color-option-5" class="color-option hide">
  <p>content 5 </p>
</div>


$(".section-option li")
        .mouseenter(function () {
            $id = $(this).children("div").attr("class");
            $(".color-option").addClass("hide");
            $(".section-option li div").removeClass("active");
            $("#" + $id).removeClass("hide");
            $(this).children("div").addClass("active");

        })
        .mouseleave(function () {


        });

Answer №1

Here's a solution that should do the trick:

$(".section-option li").hover(function() {
    var id = $(this).children("div").attr("class").split(' ')[0];
    $("#" + id + "").toggleClass("active").toggleClass("hide");
});

To ensure it works correctly for all list items, I've included the split() method. This is necessary because the first li has two classes (color-option-1 and active).

Answer №2

It's quite simple, really. The first time you're adding the active class to your div.

Now, when you hover over it again, the div will have both the active class and the id, which can lead to selecting the wrong element.

You have two choices:

1- Add the active class to the li instead of the div

2- When retrieving the class of the div, remove the active if it exists

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

Is it acceptable to conceal items by utilizing display:none?

When dealing with a dynamic website that includes components from various plugins, is it acceptable to hide elements temporarily or permanently using display:none? Sometimes clients may request certain items to be hidden from the page, so instead of removi ...

Creating a div element that maintains a consistent aspect ratio regardless of its width or height

I've been working on making my player div responsive in terms of height. Currently, when I resize the width, the div scales down accordingly. However, resizing the height does not have the same effect. The scrollbar doesn't seem to help either du ...

Arranging an image next to a text input in Bootstrap 5: A step-by-step guide

I want to align a magnifying glass image next to a text input on the left side. However, it is currently displaying above the text input. How can I make sure they are positioned side-by-side using Bootstrap 5? <link rel="stylesheet" href="https://c ...

Having trouble with my bootstrap carousel not functioning properly - issue with jQuery line causing the rest of the code to disable

I've been facing some challenges with this Bootstrap slider for quite some time now. Despite my best efforts and thorough research on the forum, I just can't seem to make it function properly. Here's the code snippet in question: <!DOCTY ...

Is there a way to ensure that all elements on a page display properly across all screen resolutions without being cut off?

My website consists of three main elements: pictures, Cards (containing pictures and various typography), and Buttons. These elements are arranged in a column layout. The issue I am facing is that the pictures and buttons get cut off on the screen due to ...

What is the best way to update an existing cookie value using angularjs?

Currently, I am working with AngularJS. When a button is clicked, I am setting a cookie and it works perfectly fine. However, when the page is refreshed and another button click occurs, a new value is stored in the array while the old cookie value becomes ...

Using Jquery to access the grandparent element

When I have code similar to what is shown below, an element contains within 3 layers: <span class="dropdown test1"> <span class="test2" type="button" data-toggle="dropdown">hello</span> <ul class="dropdown-menu test3" style="m ...

Is there a way to align my horizontal menu with the top of the screen without encountering the hover problem?

I'm trying to make my horizontal navigation bar stay at the top of the screen. The issue is that when I set the top property to 0, the menu sticks to the top but the initial menu item is obscured when hovered over. If I remove the top property, the ho ...

I'm having trouble getting my jsonp to function properly. Can anyone help me troubleshoot?

I've been working on my website for the past two weeks, learning HTML, CSS, and a bit of JavaScript. However, I'm facing an issue with the following code that I can't seem to resolve: <head> <script src="http://ajax.googleapis.com/ ...

What is the best way to toggle the enablement of a textbox with jQuery?

Welcome all! Initially, all controls are disabled. Upon clicking the Add or New button, I want to enable textboxes and the Save button while keeping the Edit and Delete buttons disabled. Once the Save button is clicked, I wish to disable all textboxes and ...

How to prevent selection of specific months in Bootstrap datepicker

I've been attempting to disable specific months in a datepicker using the datesDisabled option. Even though the documentation states that it should work by passing an array of strings, I haven't been able to make it function properly. Here' ...

Understanding the process of extracting map data from JSON files

I am currently working with Spring 3 and JQuery. My goal is to retrieve a Map containing element IDs and their values from my Spring Controller, and then use this data to update the View. Below is a snippet of the Controller code: @RequestMapping(value= ...

Combine values in CSS without any spacing

Currently, I am attempting to create a LESS mixin that can take a numerical value (degrees for rotation) and generate the appropriate CSS code to rotate an element. However, I am facing difficulties trying to handle both "270deg" and the integer "3" (which ...

Loading texts with the same color code via ajax will reveal there are differences among them

I am currently designing a webshop with green as the primary color scheme. Everything is functioning perfectly, but I have noticed that the text within an ajax loaded div appears brighter than it should be. The text that loads traditionally is noticeably d ...

Tips for identifying the amount of <ul> elements contained within every div

Dealing with a large file structured in the same way, I am looking for a method to isolate a specific div, determine the number of ul's within it, and then iterate through each one to extract the value of every li element. <div class="experiment ...

Tips for safely sanitizing an HTML file containing both HTML tags and JavaScript functions within an Angular application

I am trying to bind an HTML file that contains both HTML and JavaScript (jQuery) functions into my Angular application. Here is how I bind my HTML file into the component's HTML: <div [innerHTML]="currentDetailEntry?.content | sanitizeHtml">&l ...

Mobile devices are failing to display the logo as expected

Can anyone help me figure out why the logo is not displaying on mobile devices? I've already tried resetting my phone's network and web browser, as well as reducing the size of the logo from 100px to 80px. Despite these efforts, the logo still wo ...

I am having trouble properly positioning an icon next to its corresponding text within a menu item

I'm a newcomer to the world of HTML5 + CSS3 and I'm having trouble with aligning menus, text, and icons within the menu. Here's the issue: Each line of the menu consists of an icon and a text description. The problem is that they are too clo ...

What's the deal with ajax constantly failing to deliver results?

Here is the ajax function that I have been working with: $.post({ url: "login", data: { nomutilisateur: nomutilisateur, motdepasseutilisateur: motdepasseutilisateur } }).done(function() { console.log("Success"); }).f ...

Problem with disabling dates on jQuery datepicker

After spending several days trying to solve this issue, I've decided to seek help. The problem at hand is disabling dates in my bootstrap datepicker using the following HTML code: <head> <!-- Required meta tags --> <meta charset="utf-8 ...