Skip to content
Advertisement

How to merge two javascript function with third function with condition?

I am beginer in js, and i have problem with merge two fucntions. I want to make third function with condition, when checkbox is marked and reCAPTCHA is marked, only then button is enable. By default, i set the button to disabled. Single functions as below is working:

  function clauseValid(elem) { 
  document.getElementById("sendBtn").disabled = false;
  return true;
  };

  function captchaValid () {
  document.getElementById("sendBtn").disabled = false;
  return true;
  };


  <input type="checkbox" name="chkbx" id='#ID#' value="#seq_claim_id#"  onClick="clauseVlid(this)">

  <div class="g-recaptcha" data-sitekey="*****..." id="ckecCaptcha" type="checkbox" data-callback="captchaValid"></div>

I tried make someone like this but it doesn’t work:

  function clauseValid(elem) {
      return true};

  function captchaValid() {
      return true};

  function CheckTest() {
   if (clauseValid(elem) && captchaValid()) {
       document.getElementById("sendBtn").disabled = false;
   }
 }

Advertisement

Answer

Use variables for keeping track of the current status of each condition:

let isClauseValid, isCaptchaValid;

function clauseValid(elem) {
  isClauseValid = elem.checked;
  setButton();
}

function captchaValid() {
  isCaptchaValid = true;
  setButton();
}

function setButton() {
  document.getElementById("sendBtn").disabled = !isClauseValid || !isCaptchaValid;
}

NB: make sure to correct the spelling mistake in your HTML onclick attribute.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement