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.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…