Steps for designing a hover effect for input and label elements

I am looking to enhance the user experience by implementing a hover effect to display images instead of wrapping them in labels. Currently, I utilize JQuery's click functionality to toggle between showing and hiding the images. However, I also want to incorporate a hover effect using the same images in their original positions.

HTML:

<div id="formone">
<input type="radio" name="starrate" value="1.0" id="b"/>
<label for="b"><img id="img1"  src="http://lorempixel.com/10/10"></label>

<input type="radio" name="starrate" value="2.0" id="c">
<label for="c" ><img id="img3"  src="http://lorempixel.com/10/10"></label>

<input type="radio" name="starrate" value="3.0" id="d"/>
<label for="d" ><img id="img5" src="http://lorempixel.com/10/10"></label>
</div>

<ul>
<li>
<img id="img2" src="http://lorempixel.com/10/10" style="position:absolute">
<img id="img4" src="http://lorempixel.com/10/10" style="position:absolute">
<img id="img6" src="http://lorempixel.com/10/10" style="position:absolute">
</li>
</ul>

CSS:

input[type="radio"] {
display: none;
}

#img1, #img3, #img5 {
width: 100px;
height:100px;
}

#img2{
bottom: 25px;
}
#img4 {
bottom: 50px;
}
#img6 {
bottom: 75px;
}

JQUERY:

$("img[id='img2']").css({"display": "none"});
$("img[id='img4']").css({"display": "none"});
$("img[id='img6']").css({"display": "none"});

    $("#img1").click(function(){
        $("img[id='img2']").show();
        $("img[id='img4']").hide();
       $("img[id='img6']").hide();
    });

    $("img[id='img3']").click(function(){
        $("img[id='img4']").show();
        $("img[id='img2']").hide();
        $("img[id='img6']").hide();
    });

     $("img[id='img5']").click(function(){
        $("img[id='img6']").show();
        $("img[id='img2']").hide();
        $("img[id='img4']").hide();
    });

View the example here

Answer №1

Utilize data attributes for this purpose.

$('ul img').hide();

$('label img').hover(function() {
  var target = $(this).data('target');
  $(target).show();
  $(target).siblings().hide();
})
input[type="radio"] {
  display: none;
}

#img1, #img3, #img5 {
  width: 100px;
  height:100px;
}

#img2{
  bottom: 25px;
}
#img4 {
  bottom: 50px;
}
#img6 {
  bottom: 75px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="formone">
  <input type="radio" name="starrate" value="1.0" id="b"/>
  <label for="b"><img data-target="#img2" id="img1"  src="http://lorempixel.com/10/10"></label>

  <input type="radio" name="starrate" value="2.0" id="c">
  <label for="c" ><img id="img3" data-target="#img4" src="http://lorempixel.com/10/10"></label>

  <input type="radio" name="starrate" value="3.0" id="d"/>
  <label for="d" ><img id="img5" data-target="#img6" src="http://lorempixel.com/10/10"></label>
</div>

<ul>
  <li>
    <img id="img2" src="http://lorempixel.com/10/10" style="position:absolute">
    <img id="img4" src="http://lorempixel.com/10/10" style="position:absolute">
    <img id="img6" src="http://lorempixel.com/10/10" style="position:absolute">
  </li>
</ul>

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

How can I retrieve the duration of a video using the YouTube Data API?

Essentially, I am attempting to extract the video duration for each video that appears in the search results. Feel free to play around with the demo I have prepared! Additionally, when I refer to duration, I am talking about the length of the video in M/S ...

Activate flashlight on web application using JavaScript and HTML through phone's browser

I need help figuring out how to activate a phone's flashlight within a web app using JavaScript and HTML. I successfully developed a web app that scans QR codes using the phone's camera. While I managed to activate the camera, I encountered chall ...

Ways to verify a parent element when all of its child checkboxes have been selected

I have a unique HTML structure compared to other questions on SO. My goal is to have the parent checkbox automatically checked when all the children checkboxes are checked. I've created a JSFiddle with my lengthy HTML code. Can someone help me identif ...

Creating a JQuery slider that efficiently loads and displays groups of elements consecutively

Currently, I am in the process of developing an infinite horizontal tab slider using jQuery. My main objective is to optimize loading time by having the slider display only the first 10 slides upon initial page load. Subsequent slides will be loaded as the ...

AngularJS is a framework that allows for the implementation of vertical synchronous

After successfully implementing horizontal synchronous scrolling, I am now trying to achieve vertical scrolling. If anyone can assist, that would be greatly appreciated. You can find the sample where horizontal synchronous scrolling works here. e.target.s ...

What is the best way to find the index of an element with the special class "active"?

I'm having trouble retrieving the index of the active div with the class name active among a collection of divs with the class name slide. Despite trying various selectors, I haven't been successful in getting the desired result. Your assistance ...

Tips for effectively utilizing innerHTML in this particular scenario

For an assignment, we were tasked with creating a Madlib game where users input words into textfields to replace certain words in a hidden paragraph within the HTML using JavaScript and CSS. The paragraph embedded in the HTML page is as follows: <span ...

Integrate database values into your CSS by dynamically changing styles using PHP

I'm attempting to dynamically change my CSS. The CSS value I need is stored in a database table called goals in the order database, which contains id and goal_1. Here's the code snippet I'm working with: <?php $conn = mysqli_connect ...

Integrate an item into the request body utilizing the $.load() function in jQuery

Currently, I am using a POST request via jQuery's $.load function to retrieve data to display in a window. This is what I am currently doing: var testObject = { thing1: 'data1', thing2: 'data2', thing3: &a ...

Display the phrase "Kindly hold on, please do not close the windows" during Ajax loading

I have implemented a partial view in my projects. Below is the Ajax code I am using: $("#btnAdd").on("click", function () { var formData = new FormData(); var dhnFiles = $("#fileDHN")[0].files; if (dhnFiles.length == 0) { ...

Tips for making Nav.Link have equal width in React Bootstrap

Looking for a way to make Nav.Link components the same width? <Navbar bg="dark" variant="dark" fixed="bottom"> <Nav className="me-auto"> <Container> <Ro ...

Passing data to an iframe through Ajax request

I have been devoting my time to a special project, and I can proudly say that I am almost done with it. However, the hurdle I am facing right now revolves around the concept of *sending parameter to iframe*. My primary objective is to craft a basic editor ...

Is it possible to display a PHP variable using jQuery?

Within my PHP code, I have the following: 'posts_per_page' => 6, I am looking to dynamically generate that number 6 using jQuery. Depending on the screen resolution, I want to calculate the number of posts to display. While I can use jQuery ...

Choosing between setting a height of 100vh versus a minimum height of 100vh

In the development of my app, I noticed an interesting issue. When I set the background on the html with a height of 100vh and resize the viewport to cause vertical overflow, the background does not cover the overflowed content: html { height ...

Styling the `<thead>` tag in Internet Explorer 7 (IE

This code is functioning correctly in IE8, Firefox, Chrome and other browsers. However, there seems to be an issue with IE7 not recognizing the following line: .defaulttable thead{border-right:55px solid #c7c9cf;} As a result, I am seeing no border in IE ...

Finding Repetitive Words in HTML Content with jQuery or JavaScript

I've been working on a jQuery script designed to find duplicate words within an HTML text. The challenge arises when the text I want to analyze is in HTML form and needs to be embedded within the script itself. I've experimented with transferring ...

How to increase the arrow size using CSS

I am currently trying to adjust the height of the step in my registration process, but the arrow size remains unchanged. I have included the code snippet below, which I obtained from another website. Can anyone provide guidance on how to set the step arrow ...

Positioning a content item at the bottom of a flexible card, while also ensuring it is vertically centered

I am having trouble aligning a Font Awesome icon to the end of a card that resizes responsively. This is the HTML code for my card: <div class="card mt-4 mycard"> <a href="#" style="text-decoration: none; color: inheri ...

Update a single javascript variable on every rotation of the Bootstrap carousel

Within my website, I have implemented a popin/modal feature using Hubspot Messenger which includes a Bootstrap 3 carousel. Each slide of the carousel displays a different "social embed" such as Facebook or Twitter, and I am looking to adjust the width of t ...

I want to show error messages using jquery only when the username or password is entered incorrectly. How can this be achieved?

document.getElementById("buttonClick").addEventListener("click", mouseOverClick); function mouseOverClick(){ document.getElementById("buttonClick").style.color = "blue"; } $("#buttonClick").hover(function() { $(this).css('cursor',&apos ...