Create a division that can alter the background image of a different division when hovered over, even if they are not

I am seeking a solution that will function seamlessly across all browsers to reach a wider audience. I am looking for a list where each "li" element changes the background of a specific "div." You can see the desired look here.

My current code is as follows:

HTML:

    <div id="container">

        <div id="div1">

            <ul>

                <li>item1<li>
                <li>item2<li>
                <li>item3<li>
                <li>item4<li>
                <li>item5<li>
                <li>item6<li>

            </ul>         

        </div>

        <div id="div2>

        </div>

    </div>

CSS:

#div1 {
    float: left;
    width: 50%;
    height: auto;
}

#div2 {
    background-size: 100%;
    height: 422px;
    width: 50%;
    float: right;
}

#container {
    min-width: 768px;
    max-width: 991px;
    margin-left: auto;
    margin-right: auto;
    overflow: auto;
}

The two "divs" are already positioned next to each other. If there is an error in my setup, please let me know. I want the list items to change the background image of the div on the right. However, since the list is not the parent element of the div, I'm struggling with finding a solution using jQuery. Thank you for any assistance you can provide!

I apologize if this question has been asked before and appreciate your patience.

EDIT:

You can see a better concept image here.

Answer №1

Learn how to use jQuery with this simple example:

// Set up event handlers for hovering over different list items
$('ul').on('mouseover', 'li:eq(0)', {bg:'background_url_1'}, onHover);
$('ul').on('mouseover', 'li:eq(1)', {bg:'background_url_2'}, onHover);
$('ul').on('mouseover', 'li:eq(2)', {bg:'background_url_3'}, onHover);
..

function onHover(e)
{
   $('#div2').css({background: 'url(' + e.data.bg + ')'});
}

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

Having difficulty transferring image files to a MySQL database through ajax jQuery in PHP

I'm running into an issue where I can't seem to upload image files to my MySQL database after submitting the form. Whenever I try, I receive this error message: Uncaught TypeError: Illegal invocation This error is showing up in the console ta ...

Utilizing the JSON result of an AWS MySQL query to display data as a DataTable on a webpage

I recently managed to link a website to a Lambda function via AWS API Gateway to execute a SQL query on an Aurora Serverless MySQL database. The JSON response is currently displayed in string form on the webpage, appearing like this: https://i.sstatic.net ...

Difficulty with collapsing icon in Bootstrap's navigation bar

I am in the process of building a bootstrap website and facing an issue with the collapsible navigation bar. The nav toggle button is always visible, but I want it to only appear when the nav bar goes to a medium size. How can I achieve this? The code sni ...

Executing numerous AJAX requests using jQuery

When I make ajax calls to two different pages, one continuously sends the response while the other keeps loading and only responds when finished. This is my approach: $('.container').html('Please wait... Working : <span id="done">0&l ...

numerous requirements for formatting utilizing Css modules

Can someone help me figure out how to set multiple conditions using a ternary operator to style an element with Css modules? I'm struggling to find the right syntax. Is it even possible? import style from './styles.module.sass' const Slider ...

Creating lasting placeholder text with gaps between each word

Looking to create a unique text input with static content on the right and editable text on the left https://i.stack.imgur.com/K0YfM.png Any suggestions? ...

Ways to retrieve information from a different server utilizing AJAX

I am in need of fetching data from a file that is located at a different URL. Below is the code snippet that showcases the AJAX request being sent to the server. <script> alert('return sent'); $.ajax({ type: "POST", ...

Utilize Javascript or Jquery to intercept and handle both GET and POST requests

Is there a method to effectively intercept and capture both GET and POST requests as they are sent from the browser to the server? In my web application, full page refreshes occur after each request is submitted, however, some pages experience delays in r ...

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...

Dealing with duplicate entries on MySQL using AJAX

After successfully entering data into a database and displaying it on a table, I wanted to incorporate a message for possible duplicates. However, when the PHP script returns a "duplicate found" message, it disrupts the flow of the table scheme. My AJAX c ...

Retrieve data from two different dropdown menus using jQuery

One of my pages is a simple PHP file named (time.php) <?php if($_GET['region'] == "europe" and !isset($_GET['timeformat'])){ $array = array("Berlin" => "15:30", "Paris" => "14:30 ...

Encountering an error message stating, "Unable to assign value to 'onclick' property"

Currently, I am at a beginner level in Javascript. While working on some exercises, I encountered an issue with the 'onclick' function as mentioned above. Despite going through other queries in this forum, I have not found a solution that works ...

troubles arise when using undeclared functions in javascript

Recently, I've been working on implementing a Javascript CountDown Timer triggered by a button click. The Javascript code is stored in an external file, as well as the CSS for styling the timer. The problem arose when I tried including the Javascript ...

Assigning a value and specifying the selected attribute for an options tag

Trying to understand the challenge of setting both a value and a selected attribute on an options tag. Each one works independently, but not together. For example: <select> <option *ngFor="let item of items" selected [ngValue]="item"> ...

Tips for utilizing Bootstrap validator to check the size and type of files

Currently, I am utilizing the Bootstrap validator to validate my form. However, I have encountered a challenge with an input field for files where I would like to implement custom validation for file types and sizes (such as pdf, docx, doc...). <div c ...

Raising the css value for each element that is impacted

I am faced with an infinite number of elements that I need to arrange next to each other. Each element has a class called "box". My goal is to separate each box by 10px increments, meaning the first element will have a left property of 0px, the second 10px ...

Is there a way to configure my datepicker so that it displays only dates that are later than a specified date

At the heart of my inquiry lies a dilemma: I have two date pickers, one for leave_start and the other for leave_end. If an individual selects "YES" for a future text_field, I aim to ensure that the date pickers only display dates after the person's an ...

Discovering the clicked element using JavaScript: A complete guide

Let me start by saying that I have come across similar posts about tracking event listeners, but in my specific case, I am struggling to figure it out. While I am familiar with the event.target property, I just can't seem to make it work. Here is a s ...

Tips on keeping the size of a react-bootstrap modal constant and enabling scrolling instead of expanding

<Modal size="lg" scrollable show={showInvModal} onHide={handleCloseInvModal}> <Modal.Header closeButton> <Modal.Title>Check out our latest items!</Modal.Title> </Modal ...

How to change from using position: sticky to fixed after scrolling to a specific div

Is there a way to transition the position of sticky content from sticky to Fixed while scrolling down and moving to the next rows, keeping it fixed until just before the footer, where it should scroll again? For example, <!DOCTYPE html> <html ...