Skip to content

Instantly share code, notes, and snippets.

@Alireza2n
Last active September 3, 2023 19:21
Show Gist options
  • Save Alireza2n/c5d92739e8677a1bfc22e1404ee22fd1 to your computer and use it in GitHub Desktop.
Save Alireza2n/c5d92739e8677a1bfc22e1404ee22fd1 to your computer and use it in GitHub Desktop.
django-simple-captcha ajax refresh button
/*
* Credits go to https://stackoverflow.com/a/20371801/8504344, https://stackoverflow.com/users/179024/mw
*/
$(document).ready(function () {
// Add refresh button after field (this can be done in the template as well)
$('img.captcha').after(
$('<a href="#void" class="captcha-refresh"><i class="fa fa-refresh"></i></a>')
);
// Click-handler for the refresh-link
$('.captcha-refresh').click(function () {
var $form = $(this).parents('form');
var url = location.protocol + "//" + window.location.hostname + ":"
+ location.port + "/captcha/refresh/";
// Make the AJAX-call
$.getJSON(url, {}, function (json) {
$form.find('input[name="captcha_0"]').val(json.key);
$form.find('img.captcha').attr('src', json.image_url);
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment