How to add class with jquery after dynamic loading is complete?

I need to add a class to <a>, which is added programmatically. When I call addClass, it does not add it, I assume that the plugin has not yet completed the installation of all its bits and parts, so the anchor does not exist yet.

The page is here . This is the login bar at the top. I want to add a class to the login and registration buttons, for example.

jQuery('.bp-login a').addClass('simplemodal-login');
jQuery('.bp-signup a').addClass('simplemodal-register');

HTML for this section

<ul class="main-nav">
<li class="bp-login no-arrow">
    <a href="http://nicolaelvin.com/community/wp-login.php?redirect_to=http%3A%2F%2Fnicolaelvin.com%2Fcommunity">Log In
    </a>
</li>
<li class="bp-signup no-arrow">
     <a href="http://nicolaelvin.com/community/register/">Sign Up
     </a>
</li>
...</ul>
+3
source share
2 answers

Try:

jQuery(function() {

    var modal_check = setInterval(function(){

        if(jQuery('.simplemodal-login').length > 1){
            clearInterval(modal_check);
            jQuery('.bp-login a').addClass('simplemodal-login');
            jQuery('.bp-signup a').addClass('simplemodal-register');
        }

    },100);

});​

This waits until dom is ready, and then checks every 10th second to see if the modal plugin is loaded. After loading the plugin, it stops the interval and adds the class.

+2
source

, DOM .

$(function () {
  $(element).addClass('hello');
});
-1

All Articles