Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

37
Views
Function with arguments not working when used inside event listener

I have 2 radio buttons (YES) (NO) and an input (PLEASE EXPLAIN) The input is hidden and the intention is to make it visible only if you select (YES) I want to create a function with parameters so that it can be reused. I don't know what's wrong with my code looks correct to me but the input field doesn't appear if you click (YES) and I see no errors

HTML CODE

<div>
  <strong class="input-group-title">Have you had any surgeries?</strong>
</div>
<div>
  <div>
    <input type="radio" id="any_surgeries_yes" value="Yes">
    <label>Yes</label>
  </div>
  <div>
   <input type="radio" id="any_surgeries_no" value="No">
   <label>No</label>
  </div>
</div>

<div class="pop-up-field toggle-content" id="explain-surgeries">
   <input type="text" name="please-explain">
   <label>Please Explain</label>
</div>

CSS CODE

.toggle-content {
    display: none;
}

.toggle-content.is-visible {
    display: block;
}

.pop-up-field {
    /*empty*/
}

JAVASCRIPT CODE

const conditionalToggler = function(condYes,condNo,toggableField) {
    if (condYes.checked) {
      toggableField.classList.add('is-visible');
  
      } else if(condNo.checked){
      toggableField.classList.remove('is-visible');
      }
    };
  

const surgeriesYes = document.getElementById('any_surgeries_yes');
const surgeriesNo = document.getElementById('any_surgeries_no');
const explainSurgeries = document.getElementById('explain-surgeries');


surgeriesYes.addEventListener('change',conditionalToggler(surgeriesYes, surgeriesNo, explainSurgeries));
surgeriesNo.addEventListener('change',conditionalToggler(surgeriesYes, surgeriesNo, explainSurgeries));
7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.