I am getting 0 values for my array when I try to add DOM elements from a webpage, how to add values to lists in .each function?

16 views Asked by At

On console it presents just empty array and 0's for all the elements on the Business Insider page with the specific selectors. How can I add each number (which is views on their site) as a list or string of numbers and then .length on the variable and push it to my html interface?

var request = require('request');
var cheerio = require('cheerio');
var bilist = new Array();
//var x = document.getElementById('bi-stats').innerHTML;

    var url = 'http://www.businessinsider.com/moneygame';
    request(url, function(err,resp,body) 
    {


            if (err)
                throw err;
            $ = cheerio.load(body); //create the dom object string
            $('span.hot').each(function() {
                $(this).text().append(bilist);
                console.log(bilist);
                console.log(bilist.length);


            });  
        }); 


function start() {        
        window.addEventListener('load', bi_list(), false);
}
1

There are 1 answers

0
asharma On

So jQuery has a different syntax for the 'for' iterator.

The correct code is shown below which appends the text element that contains # of views of the site Business Insider.

$('span.hot').each(function(i, elem) {
            bilist[i] = $(this).text();

       });