I am brand new to LWC and am trying to implement Google reCAPTCHA V2 into our community. I used the following guide as a tutorial but am noticing issues with some of the code: https://www.learnexperiencecloud.com/s/article/Implementing-reCAPTCHA-in-Community-Cloud#LWCSupport

Anyways, everything is working except the ability to enable/disable a lightning button on successful verification. I believe this is just a simple issue with how I'm trying to implement. Basically a listener is added into the connectedCallback on the event the user clicked the reCAPTCHA checkbox. I am then firing off a POST call to the recaptcha/api/siteverify endpoint for an extra security measure. On successful response, I want to enable the lightning button. Below are the snippets of code. Everything is working except where I have the comments in capital letters. I have tried instead of calling this function to just set this.disableButton = false, but that doesn't work either (also commented out for now). The button remains disabled in both cases.


    <h2>reCAPTCHA V2</h2>
    <div class="recaptchaCheckbox"></div>
    <lightning-button label="Submit" disabled={disableButton}></lightning-button>


import { LightningElement, track, api, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import verifyRecaptcha from '@salesforce/apex/recaptchaLWCController.insertRecord';

export default class recaptchaLWC extends NavigationMixin(LightningElement) {
    disableButton = true;

    // Runs when component loads
    connectedCallback() {
        document.addEventListener("grecaptchaVerified", function(e) {
            verifyRecaptcha({ record: null,
                recaptchaResponse: e.detail.response})
                .then(result => {
                    //this.disableButton = false; // THIS DOESN'T ENABLE THE BUTTON?
                    this.handleButton(); // WHY CAN'T I CALL THIS?
                .catch(error => {

    handleButton() {
        this.disableButton = false;

    renderedCallback() {
        var divElement = this.template.querySelector('div.recaptchaCheckbox');
        //valide values for badge: bottomright bottomleft inline
        var payload = {element: divElement, badge: 'bottomright'};
        document.dispatchEvent(new CustomEvent("grecaptchaRender", {"detail": payload}));

I appreciate any support... thanks!

1 Answer 1

document.addEventListener("grecaptchaVerified", function(e) {

Should be:

document.addEventListener("grecaptchaVerified", (e) => {

When you use a normal function, this can become unpredictable. By using the arrow function (=>), this will be set to the appropriate object (your class).

  • 2
    Awesome! I learned something new! This worked great thanks! Feb 18, 2022 at 19:36
  • When I implement this, how can I consume or call from my community page?
    – user120111
    Jul 26, 2022 at 3:56
  • @user120111 I haven't tried this in a Community, so I don't know exactly how to answer your comment. I suggest you ask a new question and include as much detail as necessary so that we can help you with your problem.
    – sfdcfox
    Jul 26, 2022 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .