Hover-over div's to switch them on

I am faced with a challenge involving a UL list structured like this:

<ul>
<li id="firstdiv">First div</li>
<li id="seconddiv">Second div</li>
<li id="thirddiv">Third div</li>
....
</ul>

Below the list, there are corresponding div elements as follows:

<div id="firstdiv">Content Here</div>
<div id="seconddiv">Content Here</div>
<div id="thirddiv">Content Here</div>

I am investigating how I can display each div only upon hovering over its respective LI item, perhaps even incorporating a fadein effect. I have attempted various solutions found online, but unfortunately, none have been successful so far.

Answer №1

Ensuring that each id is unique is crucial, so alternative methods of referencing your <div>s must be utilized. One suggestion is to make use of data-* attributes:

HTML

<ul>
    <li data-id="firstbox">First box</li>
    <li data-id="secondbox">Second box</li>
    <li data-id="thirddbox">Third box</li>
</ul>

Subsequently, your jQuery script could resemble the following example:

$('ul li').on({
    'mouseenter':function(){
        $('#'+$(this).data('id')).fadeIn();
    },'mouseleave':function(){
        $('#'+$(this).data('id')).fadeOut();
    }
});

Example Link

Answer №2

Please refer to this particular fiddle.

It's important to ensure that the ID's remain unique.

Below is an excerpt of code extracted from the aforementioned fiddle:

$(document).ready(function() {
    $('ul>li').hover(function(){
        $('#d'+$(this).prop('id')).show();
    }, function() {
        $('#d'+$(this).prop('id')).hide();
    });
});

Answer №3

Update your identifier to a class

<ul>
<li class="first-div">First div</li>
<li class="second-div">Second div</li>
<li class="third-div">Third div</li>
</ul>

<div class="first-div">Content 1 Here</div>
<div class="second-div">Content 2 Here</div>
<div class="third-div">Content 3 Here</div>

then you can implement:

$('ul li').hover(function() {
    var cls = $(this).attr('class');
    $('div.'+cls).toggle();
})

View Demo on Fiddle

Answer №4

Give this a try.

This is the HTML code:

<ul>
<li id="firstli">First item in list</li>
<li id="secondli">Second item in list</li>
<li id="thirdli">Third item in list</li>
</ul>

<div id="firstdiv">Content for first item here</div>
<div id="seconddiv">Content for second item here</div>
<div id="thirddiv">Content for third item here</div>

Here is the JavaScript code.

$(document).ready(function(){

 $("#firstdiv").hide();
 $("#seconddiv").hide();
 $("#thirddiv").hide();

    $("#firstli").hover(function(){
      $("#firstdiv").show();
    });
    $("#secondli").hover(function(){
      $("#seconddiv").show();
    });
    $("#thirdli").hover(function(){
      $("#thirddiv").show();
    });
}); 

Check out the output here: fiddle demo

Answer №5

It is important to assign unique identifiers to elements

<ul>
<li id="firstElement">First element</li>
<li id="secondElement">Second element</li>
<li id="thirdElement">Third element</li>

</ul>

<div class="myNewClass" id="firstElement1">ContentA Here</div>
<div class="myNewClass" id="secondElement1">ContentB Here</div>
<div class="myNewClass" id="thirdElement1">ContentC Here</div>

$(".myNewClass").hide();
$("ul li").hover(function(){
$(".myNewClass").hide();
$("#"+this.id+1).show();
});

See the Demo

Answer №6

Here is the solution you've been looking for

<html>
<head>
<style>
.hidden_div{
    display: none;
    position: absolute;
}
</style>
</head>
<body>

<ul>
<li id="firstdiv">First div</li>
<li id="seconddiv">Second div</li>
<li id="thirddiv">Third div</li>
</ul>

<div class="firstdiv hidden_div">Content 1</div>
<div class="seconddiv hidden_div">Content 2</div>
<div class="thirddiv hidden_div">Content 3</div>

<script src="script/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
    $("li").mouseover(function(){
        var id=$(this).attr('id');

        $('.hidden_div').fadeOut();
        $("."+id).fadeIn();
    })

});
</script>

</body>
</html>

You just need to adjust where your script is placed

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

Storing the DOM in a Variable

I have saved the response of an XMLHttpRequest() into a variable from a specific website, let's say yahoo.com. How can I retrieve the values of the DOM content using either getElementById or getElementsByName on this variable? For instance: var dump ...

Leveraging JavaScript and Thymeleaf to manipulate a list

I am trying to access an object from a list in JavaScript, which is being passed from the controller. Currently, I am working with Thymeleaf and Spring Boot. The list is named ${collaborateurs}. The following code snippet is functional: <script th: ...

Failure to send data via Jquery AJAX to a different webpage

I am currently in the learning phases of jQuery and Ajax. My current project involves creating a table list that filters data based on dates. However, I encountered an issue where the logic did not work as expected. Upon recreating the same logic in a sepa ...

Does the height of the rendered font adjust based on the length of words in Japanese formatting?

I can't figure out why this strange issue is occurring... It seems that the font "size" (or rather, the height it occupies) changes depending on the length of a word. I tried to reproduce this in English without success, but it consistently happens w ...

What can I do to resolve the problem with td border

Check out my website over at browsethewebclean.com I've come across a peculiar issue with the borders and background color of some nested tables on my page. It seems that the border is not aligning properly and the background color goes beyond the im ...

JQuery ajax event "on drag and drop" is not functioning as expected

I have a collection of images in a <ul><li> with unique IDs for each element. I am trying to trigger a $.ajax call after moving an image to a different location, but it doesn't seem to be working as expected. Here is the HTML code: <scr ...

Issue with the initial element in CSS and the cause remains a mystery

"first of type" dont like to work: .elements { width:70%; margin:0 auto; display:flex; flex-direction: column; .addElement { width:280px; text-align: center; border:1px solid black; font-family: ...

Using AngularJS with the Chosen Plugin to pre-select a value in a dropdown menu

After following the guidance from this URL, I successfully incorporated the chosen plugin into Angular.js. Now that I can retrieve the value, my next objective is to have the selected value be pre-selected in the chosen dropdown menu. If anyone has a sim ...

Firefox not triggering image onload event

I have developed an image preloader that is functioning perfectly in all browsers except for Firefox (currently tested with version 10.0). Essentially, the input consists of an array of images named image_list. These images are dynamically appended to the ...

Ruby on Rails: Automatically redirecting to a new page

When I make a call to the index action from jQuery AJAX, instead of displaying the contents of the page, it automatically redirects. Notably, I do not utilize show_feeds defined in this controller. class AdAssistedController < CommonController def i ...

The precise moment for the formation as opposed to the choice of elements

To enhance the appearance of YouTube/Vimeo videos, I am utilizing a technique mentioned in this discussion, which involves creating custom thumbnails for embedded videos. The process is quite straightforward - displaying an image as a placeholder and repl ...

Is there a way to identify the specific list item that a draggable element has been dropped onto within a jQuery sortable with connected draggable lists?

Take a look at these two sets of items: user's inventory <ul id='list1'> <li>Apple</li> <li>Banana</li> </ul>available products <ul id='list2'> <li>Orange</li> ...

Switch the checked status of an input dynamically using jQuery function

It seems like I might be overlooking something quite obvious, but I can't figure out why this jquery function is behaving inconsistently. HTML: <label id="income_this_tax_year"> <div class="left"> <p>Have you had Self-Employm ...

Struggling to locate an element with jQuery's closest() method?

Currently, I am utilizing jQuery and Ruby on Rails for my project. Within a table with 3 columns, there is a form element in the form of a select box that utilizes AJAX to submit the result. My goal is to change the color of a check mark by adding or remov ...

How can jQuery be used to adjust the width and height of an iframe?

After researching a solution on a website, I attempted to implement it but encountered issues. It seems like there may be another script dictating the styling, so I need to add some code directly to the page to override it. This is what I tried: <scri ...

Angular encountered an issue with an HTTP POST request, as the 'Access-Control-Allow-Origin' header was not found on the requested resource

I have been attempting to transmit data to a servlet using Angular HTTP POST requests. var httpPostData = function (postparameters, postData){ var headers = { 'Access-Control-Allow-Origin' : '*', &a ...

Make JQuery send a POST request instead of OPTIONS when making a cross-domain call

Currently, I am facing a challenge where I need to send an ajax POST request (json) to a different domain. However, since I am testing on localhost and do not have access to the server yet, everything is a bit more complicated. Unfortunately, it is not po ...

What is the process for refreshing the vis.js function to clear or reset the selection of a node or edge?

Begin by checking out this resolved issue. Building upon that, I am encountering an issue when trying to utilize the selectNode or selectEdge function in the same page and JS file. For some reason, the function is unresponsive when clicked after the previo ...

How can text be concealed within an input field?

My degrees converter code is functioning well, but I have an issue. When I input a number for one temperature type, the conversion for the other two types is displayed correctly. However, if I then enter a number for another temperature type, the previous ...

What is the best way to retrieve an ID within a loop and utilize a modal for editing and updating data with CodeIgniter 3 and jQuery

I have created a product data display using a loop with the CI3 framework. Everything works fine when trying to edit the data using a modal, but I am facing an issue where only the data in the first row can be updated. The data in the second and subsequent ...