How to add multiple invisible recaptcha in single page?
Had the same problem. After some puzzling I got it to work.
Used the idea Alessandro provided, and made it so the form would automatically be submit on success.
<script type="text/javascript">
var onloadCallback = function() {
$(".g-recaptcha").each(function() {
var el = $(this);
grecaptcha.render($(el).attr("id"), {
"sitekey" : SITE_KEY,
"callback" : function(token) {
$(el).parent().find(".g-recaptcha-response").val(token);
$(el).parent().submit();
}
});
});
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
Below a more reliable solution to Peter and Alessandro answers when nesting elements.
<script>
$(".g-recaptcha").each(function() {
var object = $(this);
grecaptcha.render(object.attr("id"), {
"sitekey" : "6LdwRC0UAAAAAK0hjA8O4y1tViGPk9ypXEH_LU22",
"callback" : function(token) {
object.parents('form').find(".g-recaptcha-response").val(token);
object.parents('form').submit();
}
});
});
</script>
<form>
<input type="text" name="example"/>
<button id="captcha1" class="g-recaptcha">submit form 1</button>
</form>
<form>
<input type="text" name="example"/>
<button id="captcha2" class="g-recaptcha">submit form 2</button>
</form>
<script src='https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit' async defer></script>