Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
Pass var to AJAX's success event

Suppose this code:

jQuery(function($) {
    $(document).find('.answer-choice').on('click', function() {
        $(this).addClass('loading-gif');

        var form = $('form#submit_quiz');
        var quiz = form.find('input[name="quiz"]').val();
        var ticket = form.find('input[name="ticket"]').val();
        var _wpnonce = form.find('input[name="ticket_nonce"]').val();

        $.ajax({
            url: public_quiz_participation.ajax_url_answer_question,
            method: 'post',
            //dataType: 'json',
            data: {
                action: 'lottery_answer_question',
                status: 'answered_question',
                quiz: quiz,
                ticket: ticket,
                question: $(this).data("question"),
                answer: $(this).data("answer"),
                _wpnonce: _wpnonce
            },
            success: function(response) {
                console.log($(this));

                // Here, among some other things, I want to 1. remove the class 'loading-gif' from the choice the user clicked, and 2. (with the help of some DOM tree traversing) add a 'disabled' class to that answer and the rest choices (its siblings) of the particular question. Part 2. is actually easy enough. My problem is that $(this) isn't available anymore to the success event function.
            },
            error: function() {
                swal.fire({
                    type: 'info',
                    html: "<h2>" + quiz.loadResource + "</h2><br><h6>" + quiz.somethingWentWrong + "</h6>"
                });
            }
        });

    });
});

Basically my question is in a comment in the code above, but I'm pasting it below also, so that it's more readable:

Here, among some other things, I want to 1. remove the class 'loading-gif' from the choice the user clicked, and 2. (with the help of some DOM tree traversing) add a 'disabled' class to that answer and the rest choices (its siblings) of the particular question. Part 2. is actually easy enough. My problem is that $(this) isn't available anymore to the success event function.

So, how can I pass the $(this) as a variable to the success event's function?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda