0

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;
   }
 }
yoshim85
  • 3
  • 1
  • 1
    Can you please explain what exactly goes wrong? – Lemondoge Dec 03 '20 at 18:57
  • You have `onClick="clauseVlid(this)"` but the function is named `clauseValid()`. Also, where are you calling `CheckTest()`? – kmoser Dec 03 '20 at 18:58
  • @Lemondoge I mean it, the button will be enabled when two conditions are met (two marked filed - one checkbox - second reCaptcha) – yoshim85 Dec 03 '20 at 19:08
  • @kmoser I think this is wrong, that's why i ask you how to do this :) – yoshim85 Dec 03 '20 at 19:10
  • https://stackoverflow.com/questions/54876600/how-can-i-call-a-function-after-grecaptcha-execute-has-finished-executing-tr – kmoser Dec 03 '20 at 19:16

1 Answers1

1

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.

trincot
  • 317,000
  • 35
  • 244
  • 286