1

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);
                }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the response, however, how do I write this exactly? I have tried various ways to fit this into my code but nothing seems to work. I simply need a long string, not an array. I have tried doing this.html() instead of newItems.push(this); but I get an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.