LeeTee
LeeTee

Reputation: 6601

JQuery - Convert array of 'HTMLDivElement' objects to string

I filter elements on a page and then check how many items are displayed and if there's less than a certain amount I want to load more items using $.get().

I am using the isotope plugin which requires the new items to be a string, but I can only seem to get HTMLDivElement objects. How do I convert this to a string?

                var $container = $('#container'),
                        filters = {};
                $container.isotope({
                    itemSelector: '.element',
                });

                function loadMoreItems(getQuery) {
                    var newItems = [];
                    $.get(getQuery, null, function(data) {
                        container = $($container, data).eq(0);
                        if (0 === container.length) {
                            // incase the element is a root element (body > element),
                            // try to filter it
                            container = $(data).filter($container).eq(0);
                        }

                        if (container) {
                            container.find('.element').each(function() {
                                newItems.push(this);
                            });
                        }

                        alert(newItems);  //what to do to get this as a string??


                    }, 'html');
                     $container.isotope('insert', newItems, true);
                }

Upvotes: 1

Views: 4710

Answers (1)

TigOldBitties
TigOldBitties

Reputation: 1337

Not familiar with isotope, but if you need the html with all elements in one string you can simply to $(elements).html() or if you need an array with each element as a string you can do

var transformElements = [];
$.each($(elements), function(index, value){
    transformElements.push($(value).html());
})

In your particular case you can do:

var newItems = "";
if (container) {
    newItems = container.find('.element').html();
}

which will create a single string with the html from all the elements.

Upvotes: 1

Related Questions