Turning bootstrap cards into selectable radio buttons with active states using jQuery.
To do this you will need Bootstrap and jQuery.
Function to unselect (uncheck) all of the radio buttons:
function unclickRadio() {
$("input:radio").prop("checked", false);
}
Function to select (check) a radio button via its id:
function clickRadio(inputElement) {
$("#" + inputElement).prop("checked", true);
} Function to remove the active class from all cards:
function removeActive() {
$(".card").removeClass("active");
} Make card with certain id have the active class:
function makeActive(element) {
$("#" + element + "-card").addClass("active");
} On ready for a radio button being clicked and then a card being clicked:
$(document).ready(function () {
$('input:radio').change(function () {//Clicking input radio
var radioClicked = $(this).attr('id');
unclickRadio();
removeActive();
clickRadio(radioClicked);
makeActive(radioClicked);
});
$(".card").click(function () {//Clicking the card
var inputElement = $(this).find('input[type=radio]').attr('id');
unclickRadio();
removeActive();
makeActive(inputElement);
clickRadio(inputElement);
});
}); The sample HTML:
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
How to enforce only one user to register or be registered in a Laravel web…