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:
JavaScript
x
15
15
1
function clauseValid(elem) {
2
document.getElementById("sendBtn").disabled = false;
3
return true;
4
};
5
6
function captchaValid () {
7
document.getElementById("sendBtn").disabled = false;
8
return true;
9
};
10
11
12
<input type="checkbox" name="chkbx" id='#ID#' value="#seq_claim_id#" onClick="clauseVlid(this)">
13
14
<div class="g-recaptcha" data-sitekey="*****..." id="ckecCaptcha" type="checkbox" data-callback="captchaValid"></div>
15
I tried make someone like this but it doesn’t work:
JavaScript
1
12
12
1
function clauseValid(elem) {
2
return true};
3
4
function captchaValid() {
5
return true};
6
7
function CheckTest() {
8
if (clauseValid(elem) && captchaValid()) {
9
document.getElementById("sendBtn").disabled = false;
10
}
11
}
12
Advertisement
Answer
Use variables for keeping track of the current status of each condition:
JavaScript
1
16
16
1
let isClauseValid, isCaptchaValid;
2
3
function clauseValid(elem) {
4
isClauseValid = elem.checked;
5
setButton();
6
}
7
8
function captchaValid() {
9
isCaptchaValid = true;
10
setButton();
11
}
12
13
function setButton() {
14
document.getElementById("sendBtn").disabled = !isClauseValid || !isCaptchaValid;
15
}
16
NB: make sure to correct the spelling mistake in your HTML onclick
attribute.