SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    586
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Put .each values in an array

    Hello forums,

    I have this jquery code.

    Code JavaScript:
    $("#myselect select ").each(function () {
    	   var ids = $(this).attr("id");
    	   //alert (ids);
    		 });
    I want to put all the values of var id in my loop inside an array. How do I do that?

    Thanks
    Don't Panic

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Very simple, there are a couple of ways to create a new array with JavaScript but i personally prefer the shortest way possible. Below is an example of how you can push the id value into an array.

    Code JavaScript:
    var myArray = [];
     
    $('#myselect select').each(function() {
        // Push the id in "myArray"
        myArray.push($(this).attr('id'));
    });

    If you want to see the array after the loop is complete i recommend you try Firebug for Firefox as it has some of the best console logging tools i have found.

    Firebug and Logging : Firebug

  3. #3
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    586
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SgtLegend that works
    Don't Panic

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No problem


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •