I am trying to display a hidden div when two different radio buttons are selected; the radio buttons are in different groups and have different IDs. So the user will select a specific radio button to be shown this content. I am hoping to achieve this with JQuery.
I want the user to be presented with specific DIV content when they make specific radio selections but in different radio groups. Help!
<script>
$(document).ready(function () {
$('[name="group1"], [name="group2"]').click(function(){
if ($(this).attr("id") == "option1") && ($(this).attr("id") == "option2")
{
$('#test-display').show();
} else {
$("#test-display").hide();
}
});
});
</script>
<div id="test-display" class="text-white" style="display:none ;">
PLEASE SHOW ME
</div>
<div>
<input type="radio" name="group1" id="option1">
</div>
<div>
<input type="radio" name="group2" id="option2">
</div>
2
Answers
You can do this with just javascript without jQuery. (edit: I just meant to say that the same logic can be implemented in jQuery too if you only want to use that, but that’s not necessary here.)
First you need to get those radio buttons you want when enabled should show the hidden div, add event listeners to them and check if both are enabled then change the css property of the hidden div.
Check my example of code below:
Say here when we select first radio button of group 1 and third radio button of group 2 then I should show the hidden div, otherwise I shouldn’t.
The above solution is perfectly valid, but if you want to use JQuery I raise you this solution:
I added some extra radios for my testing, just to double check that any radio works. You had the right idea, but the JQuery implementation wasn’t spot on.
I wrote comments on how my solution works but here is a quick recap.