RECAPTCHA - EXPLICIT CALL DYNAMICALLY
by bizamajig
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?render=explicit&amp;hl=sv"></script>
<button id="showModal" type="button">Show Modal</button>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<div id="captcha-container"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
JavaScript
"use strict";
$("#showModal").click(function() {
$("#myModal").modal("show");
var input__recaptcha = null;
if ( $('#captcha-container #g-recaptcha-response').length > 0 ) {
alert('2');
$('#captcha-container').empty();
grecaptcha.reset( input__recaptcha );
}
else {
setTimeout(function() {
alert('1'); input__recaptcha = createRecaptcha();
}, 100);
}
});
function createRecaptcha() {
return grecaptcha.render("captcha-container", {sitekey: "6LcgSAMTAAAAACc2C7rc6HB9ZmEX4SyB0bbAJvTG", theme: "light"});
}