Ways to activate a hover effect on an external element

Hello everyone!

This is my first time posting here and I've been struggling to find a solution with my limited knowledge. I could really use some help on this:

Is there a way to trigger a hover effect on an element that is not related (no parent, child, or sibling)?

Here's the code I have:

<div id="projects">
  <section class="menu_projects">       
    <ul>
      <li class="1"><a></a></li>
      <li class="2"><a></a></li>
    </ul>
  </section>
  <section class="preview_projects">        
    <div id="preview"></div> 
  </section>
</div>

I want to change the background image of "#preview" when hovering over "Class 1" and display another image when hovering over "Class 2."

Please feel free to share any suggestions you might have whether it's CSS, JavaScript, or something else. Thank you all in advance!

Answer №1

Let's tackle this problem with some JavaScript wizardry!

I've stored the images in an array named images. By using the forEach() method, I can target each li tag individually. When the mouse moves over one of these tags, I fetch the corresponding image from the array based on its index.

Also worth noting is that your current class names for the li tags - 1 and 2 - aren't suitable for CSS styling. I've made adjustments to them, now they are li_1 and li_2.

window.onload = function() {
let ul_li = document.querySelectorAll('ul li');
let preview = document.querySelector('#preview');
let images = ['https://example.com/image1.jpg', 'https://example.com/image2.jpg'];

  ul_li.forEach(function(ul_li_current, index) {
    ul_li_current.addEventListener('mousemove', function() {
      preview.style.backgroundImage = "url('"+ images[index] +"')";
    })
  })
}
#preview {
  width: 300px;
  height: 300px;
  background-size: cover;
}
<div id="projects">
  <section class="menu_projects">       
    <ul>
      <li class="li_1"><a>image 1</a></li>
      <li class="li_2"><a>image 2</a></li>
    </ul>
  </section>
  <section class="preview_projects">        
    <div id="preview"></div> 
  </section>
</div>

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

Changing innerHTML with HTML generated by a PHP function? (utilizing xajax)

I have managed to successfully update the content inside a DIV using xajax when clicking a link. However, I noticed that it only works when I directly assign HTML within the function itself, not when calling it from another function. Allow me to demonstra ...

Incorporate the AngularJS controller into your JavaScript code

I am facing an issue with my jQuery UI dialog that contains a dynamic <select> populated with Angular and AJAX. The problem is that the AngularJS script still runs even when the dialog is not visible. To solve this, I added a condition to stop the s ...

The button click event fails to trigger after entering text into an input field

I created a basic calculator. Users can input a value in the designated field and then click on the '+' button. The cursor stays in the input field, allowing users to immediately enter a new value after clicking the '+'. The mouse poi ...

Any suggestions on how I can make my modal stand out more?

I'm having trouble getting the modal to show up in this HTML file. Do I need to add something here or somewhere else? Do I also need to write a button onclick function? The modal should display when the "delete" button is clicked. <div class=" ...

What do you call the syntax %< ... >%?

Observed zoomInAnimation : true, zoomOutScale : false, templateLegend : "<ul class=\"<%=type.toLowerCase()%>-legend\"><% for (var j=0; j<sections.length; j++){%><li><span style=\"background-color:<%=section ...

Refresh a webpage content dynamically without the need to reload the entire page

How can I implement YouTube's page navigation system that does not require the entire page to reload when switching pages? Do I need to incorporate Javascript or utilize an API for this functionality? ...

Displaying a variable within a label utilizing the syntax <%= %>

I have a string variable in the code behind and I want to display it in an HTML label. I am aware that I can control the label using ASP.NET like this: label.Text = stringVariable; However, I would like to use <%= %>, so here is what I have tried: ...

Add multiple images to various div containers

I am facing a challenge with my code as I am trying to upload and display two different files inside of two different divs using two buttons. Currently, my code is only working for one image. How can I modify the code to handle two images successfully? Any ...

What is the best way to iterate through anchor links surrounding an image and dynamically load content into the figcaption element using AJAX?

Seeking assistance for an issue related to my understanding of the $this keyword in jQuery. I am facing a problem where I have 3 images on a page, each wrapped in an anchor link. My goal is to loop through these links, retrieve the URL of each anchor lin ...

Reducing the size of a Form Fieldset text once an option has been selected

I’m brand new to the world of Javascript and Jquery and I'm currently working on designing a unique questionnaire-style webpage. My goal is to pose a question, then once an answer has been selected, shrink the text and display the next question in ...

binding swipe action to click event

I am utilizing images from a database and implementing next and previous buttons for pagination. What I aim to do is connect the click events of these buttons to swipe actions, so that swiping from right to left triggers the next link, while swiping from l ...

Refresh your webpage automatically without the need to manually refresh after clicking a button using AJAX in HTML and PHP!

One issue I'm facing is that the page doesn't auto-refresh, although it loads when I manually refresh it. Below you can find my HTML and AJAX code along with its database details. The Trigger Button <?php $data = mysqli_ ...

The CSS import for fonts is causing no change in the font appearance

Hello there, I've just begun working on some CSS projects and I'm facing an issue with my font import. Despite writing the code below for the font import, I'm not seeing any change in the font: @font-face { font-family:gamefont; src:url(raw ...

ways to remove a specific category from a search

Recently, I came across some code that displays the title headings of the latest 10 WordPress posts from a database. However, I need to modify this code to exclude a specific category. Can anyone provide assistance with this? <?php require_once 'n ...

Animating Sliding Images with jQuery in a Sleek Sliding Page

Just completed a 5-page sliding site, each page featuring an image slider. Utilizing an image slider within a page slider. However, when attempting to use the same image slider with different images for page slide 3, it's not functioning. Any assista ...

"Dilemma with Displaying a 3-Digit Number in DaisyUI Using Next.Js Countdown Component

Hey there, I'm encountering an issue with the countdown component on my Next.js website using daisyUI. When the number of days exceeds 99, it's not rendering correctly as shown below: https://i.sstatic.net/cWR2tSEg.png https://i.sstatic.net/V0Iv ...

Generating Dynamic Widgets Within the Document Object Model

Currently, I am working with jQuery Mobile 1 alpha 1. In order to create a text input field using jQuery Mobile, you need to include the following HTML code: <div data-role='fieldcontain'> <label for='name'>Text Input:</ ...

Populate div with straight lines running vertically or horizontally

I'm wondering how to create vertical or horizontal lines inside a div. I tried this in the past but lost the code, and searching for "fill div with lines" isn't giving me any relevant results. Here's an image to illustrate what I'm try ...

How can you adjust the div orientation for small screens using Bootstrap?

I've been working with Bootstrap and I'm facing an issue with the orientation of a div on small screens. On medium screens, I have two columns set to 6, 6 which should stack on top of each other on smaller screens. Here's how I want it to lo ...

Bulma's Grid System Spans Columns Across the Entire Viewport

Utilizing the Bulma CSS framework. In my attempt to arrange some elements, I am puzzled as to why, in a desktop view, the items do not transition to the next row once they exceed the viewport. Below is a snippet of code along with a link to a JS Bin exam ...