Insert jQuery after callback?

This does not work:

$block.insertAfter(form);

$('.date', $block).datetime({
     userLang: 'en',
    americanMode: true
});

but it does:

$block.insertAfter(form);

window.setTimeout(function() {
    $('.date', $block).datetime({
        userLang: 'en',
        americanMode: true
    });
}, 1000);

datetime()is a plugin that I attach to an input element with a class date. Obviously, # 1 does not work, because this element is still not available in the DOM, waiting for 1 second of work. But it's hacked. How can I extend jQuery to accept a callback for a method insertAfter()or in some other way?

+5
source share
1 answer

Wrap your code in a document-ready spell:

$(function() {
    $block.insertAfter(form);

    $('.date', $block).datetime({
         userLang: 'en',
        americanMode: true
    });
});

And make sure this happens after loading the date and time library. That is, this tag <script>should be after <script src="datetime.js">. And make sure the attribute is asyncnot set in the tags <script>.

+2
source

All Articles