To enhance user experience, allow users to click on the label associated with the radio button. This triggers an action in the surrounding div, giving the impression that the div itself is being clicked. Remember to use the "for" attribute in the label to link it to the input's id.
By assigning the same name attribute to all radio buttons and distinct ids (e.g., name="myradio"), clicking one will deselect others and select only the clicked option. This consistent naming convention enables you to retrieve and utilize this data as needed.
CSS plays a crucial role in this setup. By targeting checked inputs (input[name="myradio"]:checked) and using the "+" selector to target the subsequent element (.clickable or .clickable .checked-box), we achieve the desired effect.
Included in the example is a snippet of jQuery code that logs the id of the selected box in the console for data tracking purposes.
For further information on the CSS selector used, refer to this link:
https://www.w3schools.com/cssref/sel_element_pluss.asp
<!DOCTYPE html>
<html>
<head>
<title>Code Sample</title>
<style>
// CSS styles omitted for brevity
</style>
</head>
<body>
<!-- Code sample for inline product selection boxes -->
<div class="div-inline-list">
<div class="product-container">
<input id="prod1" type="radio" name="myradio">
<label for="prod1" class="clickable"><span class="checked-box">✔</span></label>
<p class="product">Prod 1</p>
</div>
<div class="product-container">
<input id="prod2" type="radio" name="myradio">
<label for="prod2" class="clickable"><span class="checked-box">✔</span></label>
<p class="product">Prod 2</p>
</div>
<div class="product-container">
<input id="prod3" type="radio" name="myradio">
<label for="prod3" class="clickable"><span class="checked-box">✔</span></label>
<p class="product">Prod 3</p>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input[name="myradio"]').change(function(){
console.log("#" + $('input[name="myradio"]:checked').attr('id'));
});
});
</script>
</body>
</html>