Jquery, Help on Array

Discussion in 'jQuery' started by Legithenry, Apr 28, 2015.

  1. #1
    hello all, how do I create an Array of a list of items from a webpage
    like say; a web page contains list(ordered pr unordedred) of countries and I want to create an Array with those countries.

    know that, I don't know the Number of countries available at the page.
    so I want my code to get the Number of countries and create an array with them.
     
    Legithenry, Apr 28, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just find whatever container has the countries you want, and do something like this: (assuming the countries are in a list with an id matching #countrylist tag)
    
          $(document).ready(function() {
             var countryList = [];
             $('#countrylist li').each(function() {   
               countryList.push($(this).text());
             });
             console.log(countryList);
           });
    
    Code (markup):
    That will iterate through each list-item in the ul, and add the text from the list-item to the array.
     
    PoPSiCLe, Apr 29, 2015 IP
  3. Legithenry

    Legithenry Active Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    63
    #3
    thanks
     
    Legithenry, Apr 29, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    You don't need the console.log-line, btw - I just added it to check output
     
    PoPSiCLe, Apr 29, 2015 IP
  5. Legithenry

    Legithenry Active Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    63
    #5
    already did
     
    Legithenry, May 1, 2015 IP