How to enable/disable Google's reCaptcha widget?

14,351

I found a solution thanks to this answer.

Add this CSS:

.disabled-element {
    opacity: 0.65;
    pointer-events: none;
}

Add the disabled-element class to the div containing the reCaptcha element:

<script>
  var onloadCallback = function() {
    alert("grecaptcha is ready!");
  };
</script>

<div class="disabled-element" id="captcha"></div>
...
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

And finally, whenever you're ready to enable the reCaptcha element, remove the class:

$("#captcha").removeClass("disabled-element");
Share:
14,351
reformed
Author by

reformed

The Lord Jesus Christ saved me in 2005 Served in the U.S. Marines (2006-2010) B.S. in Computer Science "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian Kernighan

Updated on June 26, 2022

Comments

  • reformed
    reformed about 2 years

    Is there a way to programmatically enable/disable Google's reCaptcha widget? The purpose for this would be to prevent a user from clicking the "I'm not a robot" checkbox prematurely.