Search code examples
javascriptjqueryloopsforeachgetjson

Jquery for each in HTML string


I have an HTML string, which I get from using getJSON method. I want to iterate through this whole string, find all divs with class product-review and push its content to my array. What is the best way to do this?


Solution

  • 1st : you need to select the 'div.product-review' by using .find()

    $(response).find('div.product-review')
    

    2nd: you need to loop through them and push its content to the array

    var contentarray = [];
    $(response).find('div.product-review').each(function(){
        var ThisIt = $(this);
        contentarray.push(ThisIt.text());
    });
    console.log(contentarray);