Alex Pho Alex Pho - 6 months ago 32
jQuery Question

Show modal on click from radio button input

I have two radio buttons. Yes and No. For yes, on click it will show a download button underneath which works fine with the current HTML and Javascript. Now when you select the "No" radio button, I want it to display a modal with information. I already have the modal implemented and working as a separate button, but I haven't found any solutions to show the modal upon radio button input for no.

Here is the current Javascript that I have for show/hide from radio button input for divs.

$(document).ready(function(){
$("input[name$='bn']").click(function(){
var radio_value = $(this).val();
if(radio_value=='0') {
$("#contact").fadeIn("slow");
$("#dlcert").fadeOut("slow");
}
else if(radio_value=='1') {
$("#dlcert").fadeIn("slow");
$("#contact").fadeOut("slow");
}
});
$('[name="bn"]:checked').trigger('click');
});


jSFiddle

Answer

You should use $('#myModal').modal('show'); if the input no is checked

$('input[name="bn"]').change(function() {
   if($(this).is(':checked') && $(this).val() == '0') {
        $('#myModal').modal('show');
   }
});

Demo: https://jsfiddle.net/IA7medd/pd86o68u/2/