Start Jquery function when a div receives a class

Asked

Viewed 72 times

0

I have a div class="modal-load" with a counter uploader that will be shown when the user finalizes a payment, but I need that counter to start only when the class is added .active in it. For this, I created a function in the JS that runs this load. I believe that this function should be rotated only after receiving the informed class.

I tested as below:

$(document).ready(function() {
    if ($(".modal-load").hasClass('ativo')) {
        startLoader();
    }
});

When I leave the class already inline and update the page works perfectly. However, when I leave to add the class by the console, nothing done!

  • But there you are testing if the modal-load have the class active, but, you haven’t added the active class yet.

  • @Leandrade I need to say that when the class is added, it runs the function. How?

  • Which event adds the class? In this same event you execute the function.

  • @I guess I’ll just have to do that myself!

1 answer

0

$(document).ready(function() {
    $(".modal-load").addClass("ativo");   //adiciona a classe ativo

    if ($(".modal-load").hasClass('ativo')) {  //se foi adicionada executa o loader
        startLoader();
        console.log('Sucesso!');
    } else {
        console.log("Não tem classe ativo");
    }
});

Browser other questions tagged

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