Currently, I am attempting to show specific fields based on the selected radio button, and it seems like I am close to the solution. However, despite my efforts, the functionality is not working as expected and no errors are being displayed.
I have defined all the necessary DOM elements, set up change event listeners, and called them accordingly. Additionally, I have set a default display property of none for a class that should work.
const intSelect = document.querySelector('#internal_form');
const extSelect = document.querySelector('#external_url');
const intForm = document.querySelector('.internal_form');
const extForm = document.querySelector('.external_form');
function show() {
intSelect.addEventListener('change', showOne);
extSelect.addEventListener('change', showOne);
}
let showOne = event => {
event.preventDefault();
showTheThings(event.currentTarget, intForm, extForm);
}
let showTheThings = (target, div) => {
if (target.checked = true) {
div.classList.remove('hide');
} else {
div.classList.add('hide');
div.querySelectorAll('input[type="radio"]').forEach(function(el) {
el.checked = false;
});
}
};
.external_form.hide,
.internal_form.hide{
display: none;
}
<form>
<div class="form-group radio-format">
<label>Method</label>
<div class="col-sm-10">
<input class="trigger form-check-input" type="radio" value="Internal Form" name="event[registration]" id="internal_form">
<label class="form-check-label" for="internal_form">Internal Form</label>
<input class="trigger form-check-input" type="radio" value="External URL" name="event[registration]" id="external_url">
<label class="form-check-label" for="external_url">Internal Form</label>
</div>
</div>
<div class="internal_form hide">
<h2>Internal</h2>
</div>
<div class="external_form hide">
<h2>External</h2>
</div>
</form>
Despite my best efforts, nothing seems to be happening as intended. While testing in my browser, I noticed that changes are successfully applied when selecting the intSelect but not when clicking on extSelect (which raises some concern).
I also experimented with a more generic approach using JS:
function show(){
if (intSelect.checked =true) {
intForm.classList.remove('hide');
} else {
intForm.classList.add('hide');
};
}
This function partially works by removing the 'hide' class, but it fails to trigger the add operation as desired.
I seem to be missing something crucial here. Any insights or assistance would be greatly appreciated.