Selecting a radio button when clicking an image

Selecting a radio button when clicking an image

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Selecting a radio button when clicking an image

Selecting a Radio Button When Clicking an Image

You’re facing a common issue with selecting radio buttons dynamically. Let’s dive into your problem and find a robust solution.

The Current Approach: Why It’s Not Working

Your current code is almost there, but it has some limitations that prevent it from working as expected:

<ul id="options-31-list" class="options-list">
    <li>
        <a class="mydata" href="" data="84">
            <img class="small-image-preview" src="value_id/84/file/o2.jpg/">
        </a>
        <input class="radio validate-one-required-by-name product-custom-option" type="radio" data="84" value="84" onclick="opConfig.reloadPrice();">
    </li>
    <!-- More list items... -->
</ul>

Here are the issues:

  1. Event not triggered: The onclick attribute is only executed when you click the radio button directly, not on any child elements like the image.

  2. No event listener for images: Your JavaScript code is missing an event listener that would trigger when clicking the image.

A Better Approach Using jQuery

To solve this issue, we’ll use jQuery to attach a click event listener to the images and then select the corresponding radio button. Here’s how you can do it:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<ul id="options-list">
    <li>
        <a class="mydata" href="" data="84">
            <img class="small-image-preview" src="value_id/84/file/o2.jpg/" data-target="#radio-84">
        </a>
        <input type="radio" name="option" id="radio-84" value="84" style="display: none;">
    </li>
    <!-- More list items... -->
</ul>

<script>
$(document).ready(function () {
    $('.mydata').click(function (e) {
        e.preventDefault();
        var target = $(this).find('img').attr('data-target');
        $('.options-list :radio').prop('checked', false);
        $(target).prop('checked', true);
        opConfig.reloadPrice();
    });
});
</script>

Here’s what we’ve done:

  1. Added a data-target attribute to the image that points to the corresponding radio button ID.

  2. Used jQuery to prevent the default link behavior when clicking the image (e.preventDefault()).

  3. Selected all radio buttons and unchecked them using $('.options-list :radio').prop('checked', false);.

  4. Checked the selected radio button using $(target).prop('checked', true);.

  5. Triggered the price reload function.

This approach ensures that only one radio button is checked at a time, just like a standard radio button group, and it doesn’t suffer from the clicking limitations you observed earlier.

Remember to include the jQuery library in your project before running this code.

Conclusion

When dealing with dynamic selection of radio buttons, always ensure that you handle events correctly. In this case, using jQuery’s event listeners allowed us to capture clicks on images and apply them to their corresponding radio buttons.

This solution provides a clean and efficient way to manage radio button selection while maintaining the standard behavior of radio buttons.

If you have any further questions or need help with other dynamic UI elements in your project, feel free to explore our website for more resources.

Happy coding!

Catalog Database Configuration Pricing

Our network