Change HTML code via jQuery

Asked

Viewed 781 times

1

I’m trying to enter the following code on this page to be able to change the text that appears when the customer subscribes to the modal newsletter, which appears when the customer is about to leave the page:

$(document).ready(function(){
    $('#btn-cadastre').on("click", function() {
        $('#modal_body_popup.modal-body-center .content-popup .newsletter-send').html('<i class="fa fa-check-circle"></i> Cadastro realizado com sucesso. Compre qualquer produto, seu cupom da <strong>Corda de Pular</strong> está garantido até o final da compra.');
    });
});

Unfortunately, I don’t know for what reason, it’s not working. I’ve tried several ways and nothing. I would like some help to be able to solve this :)

PS.: I am using Tampermonkey v4.5 to do the tests.

1 answer

1

First I would put an Alert inside the click function to check if it is actually entering the function.

$(document).ready(function(){
    $('#btn-cadastre').on("click", function() {
        alert("Entrou");
    });
});

If you are entering the function you need to check that the selector is correct. In your case you have the #modal_body_popup.modal-body-center that is the parent of . content-popup parent of . newsletter-send (not necessarily 1st degree)

If you are not entering the function and the #btn-register selector is actually correct, maybe #btn-register is an item that was inserted into the page via javascript after you created your click function. In this case one of the solutions is to use the "$(Document). on":

$(document).ready(function(){
    $(document).on("click","#btn-cadastre", function() {
        $('#modal_body_popup.modal-body-center .content-popup .newsletter-send').html('<i class="fa fa-check-circle"></i> Cadastro realizado com sucesso. Compre qualquer produto, seu cupom da <strong>Corda de Pular</strong> está garantido até o final da compra.');
    });
});
  • I ran the tests if you were entering the function, and you’re not. Unfortunately I have no advanced knowledge of jQuery to solve this problem right away. But I tried to do it the way you told me, using $(Document). on and it didn’t work either.

  • I believe that the code I am putting, is coming before the modal code entered. How can this situation be circumvented?

  • In this case check the browser console for javascript errors. Click F12 and go to the Console tab. Paste the error here, if any

  • Remembering that the jquery library must be referenced for the commands to work

  • Unfortunately on the console there is no error. And the library is already referenced, as I make constant use of the library on the page.

  • I did a test inserting the code in the console after all the page loading and so it works. So I tried to make use of the code after all loading the page, but it didn’t work. Nor with setTimeout

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.