defining g-recaptcha callback function dynamically

http://stackoverflow.com/questions/42671854/passing-variables-in-invisible-recaptcha-callback

by Vivek Athalye

HTML

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<button id='submit'>
Submit
</button>
<div id='recaptcha' data-callback='onSubmit'></div>

JavaScript

$(function(){
	$('#submit').click(function (e) {
			e.preventDefault();
			var that = $(this);
			if(validate(that)) {
				$('#recaptcha') // use ID of your .g-recaptcha div
				window.onSubmit = function(){ // set it dynamically here
					console.log('called back with', that); // you can access `that` here
				}
				setTimeout(simulateGRecaptchaExecute, 2000);
			}
	})
});

function validate() {
	return true;
}

function simulateGRecaptchaExecute() {
  console.log('going to call: ' + $('#recaptcha').attr('data-callback'));
	window[$('#recaptcha').attr('data-callback')].call(null);
}