Select the radio button by hovering the mouse over it

Is there a way to check my radio buttons when hovering over them without having to click? I want to display photos based on the selected radio button. I attempted this using JQuery, but I am still learning and consider myself a beginner.

Answer №1

To make the radios respond to the mouse entering, you can listen for the mouseenter events. At this point, you have the option to either simulate a click event on the current radio button or explicitly set the checked property of the radio to true. It's important to note that radios and checkboxes should utilize .prop() instead of .attr() as they don't technically possess a value in HTML; they are solely named properties.

$(function() {
  $(':radio').on('mouseenter', function() {
    $(this).prop('checked', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="radio-group" value="radio1"><br/>
<input type="radio" name="radio-group" value="radio2"><br/>
<input type="radio" name="radio-group" value="radio2"><br/>

Answer №2

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Interactive Radio Buttons</title>
  <style>
   </style>
</head>
<body>
  <label>
    <input type="radio" name="photo" data-image="http://placehold.it/350x150" class="radio-hover">
    Option - 1
  </label>
  
  <label>
    <input type="radio" name="photo" data-image="http://placehold.it/250x150" class="radio-hover">
    Option - 2
  </label>
  
  <br>
  
  
    <img src="http://placehold.it/350x150" class="photo1" alt="">


  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  
  <script>
    $(".radio-hover").hover(function(){
      var imageUrl = $(this).data("image");
      $("img").show();
      $("img").attr("src",imageUrl);
    },function(){
      $("img").hide();
    });
  </script>
</body>
</html>

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

Javascript - issue with accurately retrieving element offset value while scrolling

My goal is to dynamically change classes for elements based on their position during scrolling. I am trying to calculate the top offset element value and adjust the classes accordingly. Here is the function I am using: handleScroll () { const header = ...

Utilize jQuery to broadcast messages containing textarea content to all users from the database via email

Currently, I am facing an issue while trying to send an email to all users from the database using the value from a textarea. It seems like there is an error in my approach. Upon further inspection, I suspect that the problem lies in selecting the data fro ...

Webpack failing to load jQuery correctly

In the process of transitioning my application.js application into smaller page bundles using SplitChunks, I have encountered a situation in my users/show.html.erb page where I am utilizing the following tag to import the specific chunk. <%= javascript ...

Intermittent AJAX 403 Forbidden errors occur from time to time

My goal here is to validate whether a username is already taken in real-time as the user types, triggering an AJAX call on keyup event to check if it's available. If the username is taken, the input field will be highlighted in red. However, I've ...

Submitting form by clicking a link on the page

To submit a POST request with "amount=1" without displaying it in the URL, I need the site to send this request when any link on the site is clicked. This JavaScript code achieves that with a GET request: window.onload = function () { document.body.oncli ...

Real-time console input/output feature for a gaming server utilizing either JavaScript or PHP

About My Minecraft Server: After running a Minecraft server in my basement for a few months, I realized that using TeamViewer to input commands and monitor the console was not ideal. The console of a Minecraft server provides a log of events with timestamp ...

Conceal the Angular Bootstrap modal instead of shutting it down

I am utilizing Angular Bootstrap Modal to launch multiple modals, including a video player. http://angular-ui.github.io/bootstrap/#/modal My goal is to maintain the video's current position even when the modal is closed. This way, when I reopen the ...

Obtaining user input from a content editable div in a React component

Get content from editable div const contentref=useRef() const handleclick=()=>{ setContent(contentref.current.value) console.log(contentref.current.value) } Element Properties <div name="textbox" ref ={contentref} cla ...

Frustrated with the endless page loading caused by three.js

I've been trying to incorporate three.js code to showcase a 3D model in GLTF format, but my webpage keeps getting stuck on pre-loading. I need assistance with displaying a preloading page until all the files are fully loaded. Any help in resolving th ...

Creating a unique icon using Jquery Mobile for a button in a PhoneGap application

Has anyone experienced issues with using a custom icon for a jQuery mobile button? I am having trouble as it is only showing a grey circle instead of the actual icon. The default icons work fine, though. Here is the code snippet from the index.html page: ...

Tips for handling numerous buttons in ionic?

I'm currently working on an app that includes surveys. In this app, users are required to answer by selecting either the Yes or No button. The desired behavior is for the chosen button to turn blue once clicked, while the other button should maintain ...

Place the Div directly above a distinct Div

Hey there! I'm currently working on positioning one div on top of another div programmatically (using javascript or css). These divs are completely separate and have the following structure: <div class="bottom"> <img src="image"></i ...

What are some possible applications for leveraging the HTML5 <link rel> attribute duo of stylesheet and next?

The specification for HTML5 emphasizes the handling of each link created within a link element as separate entities. This means that multiple link elements with `rel="stylesheet"` are treated independently, considering them as distinct external resources a ...

jQuery tip: prevent redundancy in your code by using fadeOut() efficiently

One common issue I encounter is: I have a potentially hidden element I need to perform certain actions on that element If the element is visible, then fade it out using fadeOut() Once the actions are completed, fade the element back in with fadeIn() The ...

Enable automatic indentation for wrapped text

I'm still learning the ropes here, but I'm looking to have text automatically indent when it's wrapped. Instead of this: Peter piper picked a peck of pickled peppers. It should look like this: Peter piper picked a peck of pickled pepp ...

PHP code to insert a advertisement div after every 5 rows of results

Is there a way to dynamically insert a div after every fifth row on a result page? I am currently using a script that displays 15 rows from a database with each pagination. Here is my complete script: <?php $sql = "SELECT COUNT(id) FROM table"; ...

Attempting to insert items into a storage array and subsequently outputting them using a loop

I am attempting to add "contacts" to local storage, and every time I add a new contact I want to refresh it in a jQuery list. I have successfully achieved this without using local storage. However, now I am facing a problem that I can't seem to solve. ...

Concealing a Div element without the use of Jquery or JavaScript

I have an Upper and Lower div in my HTML code. I am trying to display the Lower div only if the Upper div is present, otherwise hide it. Is there a way to achieve this using CSS without using Jquery or Javascript? Note: No modifications should be made t ...

Angular JS is encountering an issue where the promise object is failing to render correctly

I'm currently learning Angular and I have a question about fetching custom errors from a promise object in Angular JS. I can't seem to display the custom error message on my HTML page. What am I missing? Below is my HTML file - <!DOCTYPE htm ...

I am looking for a highly specialized jQuery selector that fits my exact requirements

Hello everyone, I'm encountering an issue with a jQuery selector. Here is the HTML code snippet: <div id="Id_Province_chzn"> <a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"> <span> + this.default_tex ...