Items added with appendTo () are not immediately available

I have a problem with elements added with appendTo () that were not immediately available in the DOM.

First, I read some data from a JSON file and then add some html to the div. Then I call a random shuffler plugin to show one of the added divs at a time.

    jsonUrl = "js/performers.json";

    $.getJSON(jsonUrl, function(json) {
        $.each(json.performers, function(i, performer) {
            var html = '<div class="performer_mini">';
            html += '<img src="' + performer.thumbnail + '" alt="' + performer.name + '" /><br />';
            html += performer.name + '<br /></div>';
            $(html).appendTo("div#performer_spotlight");
        });
    });

    $("#performer_spotlight").randomShuffler(".performer_mini", 3000, 3000, 9000);

      

The random shuffler does the following:

(function($) {
    $.fn.randomShuffler = function(shuffledElements, fadeInTime, fadeOutTime, timeout) {
        fadeInTime = fadeInTime || 3000;
        fadeOutTime = fadeOutTime || 3000;
        timeout = timeout || 9000;

        $(shuffledElements).hide();

        var $old_element;
        var $new_element;
        var old_index = 0;
        var new_index = 0;

        function shuffleElement() {
            $old_element = $new_element;
            old_index = new_index;
            while ($(shuffledElements).length > 0 && old_index == new_index) { // don't display the same element twice in a row
                new_index = Math.floor(Math.random()*$(shuffledElements).length);
            }
            $new_element = $(shuffledElements + ":eq(" + new_index + ")");
            if ($old_element != undefined) {
                $old_element.fadeOut(fadeOutTime, function() {
                    $new_element.fadeIn(fadeInTime);
                });
            } else {
                $new_element.fadeIn(fadeInTime);
            }
            setTimeout(shuffleElement, timeout);
        }

        $(this).show();
        shuffleElement();
    }
})(jQuery);

      

The first time the shuffleElement () function is called $ (shuffledElements) .length is 0, so the element is not displayed. On the next call to shuffleElement (), the elements added with appendTo () are available and one is chosen at random as expected. After that everything works correctly.

Is there a way to update the DOM or make these elements available to jQuery immediately after adding them using appendTo ()? Any other suggestions on how to do this?

+2


a source to share


2 answers


When do you call randomShuffler? Are you calling this function after the AJAX request completes successfully? I've always assumed that an element added by appendTo is available immediately after being added.

This code works really well for me:      



<script language="javascript">
    var test = "<div class=\"test\">1</div>";
    $(test).appendTo("div#performer_spotlight");
    $("div.test").html("<b>Hello</b>");
</script>

      

+3


a source


Yes, as @MeF Convi says you only need to call the plugin after getJSON has finished:



$.getJSON(jsonUrl, function(json) {
    $.each(json.performers, function(i, performer) {
        var html = '<div class="performer_mini">';
        html += '<img src="' + performer.thumbnail + '" alt="' + performer.name + '" /><br />';
        html += performer.name + '<br /></div>';
        $(html).appendTo("div#performer_spotlight");
    });
    $("#performer_spotlight").randomShuffler(".performer_mini", 3000, 3000, 9000);
});

      

+1


a source







All Articles