By default custom_2
, custom_3
, custom_4
divs should remain hidden.
- Only If "Radio 1" is selected div id "custom_2" is shown
- Only If "Radio 2" is selected div id "custom_3" is shown
- Only If "Radio 3" is selected div id "custom_4" is shown
- I also have other divs within the same parent DIV like custom_5, custom_6. These should NOT be affected.
I have tried below but it’s not working. Can anyone help please? Here is the jQuery:
CRM.$(function ($) {
$("input[name$='custom_1']").click(function () {
var test = $(this).val();
$("#custom_2").hide();
$("#custom_3" + test).show();
});
});
And here is the HTML:
<div class="ShowHide">
Radio 1 <input type="radio" name="custom_1" value="1" id="CIVICRM_QFID_1 " />
Radio 2 <input type="radio" name="custom_1" value="2" id="CIVICRM_QFID_2" />
Radio 3 <input type="radio" name="custom_1" value="3" id="CIVICRM_QFID_3" />
<div id="custom_2" class="crm-section custom_2-section form-item">Radio 1 Selected</div>
<div id="custom_3" class="crm-section custom_3-section form-item">Radio 2 Selected</div>
<div id="custom_4" class="crm-section custom_4-section form-item">Radio 3 Selected</div>
<div id="custom_5" class="crm-section custom_5-section form-item"></div>
<div id="custom_6" class="crm-section custom_6-section form-item"></div>
</div>
Thanks.
5
Answers
you can do this by hiding all divs using a class and then showing the div you want using id, here is an example
html:
js:
You can try show/hide based on clicked radio buttons like the following way:
Assuming that the
DIV
element to be displayed has an ID with an integer suffix greater than the value of the radio button being checked one could, using vanilla JS, accomplish the show/hide like this:The following approach uses the relationship between the values of the radio buttons (1, 2, 3) and the position of the
<div>
elements to be shown or hidden.Here’s a CSS solution using the functional
:has()
pseudo-class:and another one using
~
general sibling combinator selector