views:

84

answers:

6

Basically, I am trying to gather the IDs of every element with a specific class and place those IDs into an array. I'm using jQuery 1.4.1 and have tried using .each(), but don't really understand it or how to pass the array out of the function.

$('a#submitarray').click(function(){

    var datearray = new Array();

    $('.selected').each(function(){
        datearray.push($(this).attr('id'));
    });

    // AJAX code to send datearray to process.php file

});

I'm sure I'm way off, as I'm pretty new at this, so any advice help would be awesome. Thanks!

A: 

IT all looks good to me, the array will be populated and be available where you have placed the comment. Have faith in yourself.

murdoch
+6  A: 

You can use map() too:

$('a#submitarray').click(function(){

  var datearray = $('selected').map(function(_, elem) {
    return elem.id;
  }).get(); // edited to add ".get()" at the end; thanks @patrick
  // ajax

});

The map() method passes each index (which my example doesn't use) and element into the given function, and builds an array for you from the return values.

Pointy
@Pointy - I loooove `map()`! Just don't forget to chain `.get()` at the end, otherwise `datearray` will contain a jQuery object instead of just the array.
patrick dw
Oh right; thanks @patrick! Don't you wish jQuery had a "reduce" in the core too?
Pointy
@Pointy - You know, the only time I've come across (or thought about) reduce was when I was tinkering with CouchDB a bit. It is an interesting bit of functionality.
patrick dw
Example: what if you want a map of "id" to "value"? With "reduce", it'd be like this map example except you'd start with `{}` and add the mapping for each element. Using Prototype, you get used to it pretty quickly and it simplifies a lot of situations.
Pointy
@Pointy - Nice. I just looked at a couple of examples online. So the first argument passed would be `{}`, and you update it with each iteration? Or do you create a variable external to the `reduce` callback, and update that, like you would with `each()`?
patrick dw
@Pointy - One more thing, your code is a good refactoring, but doesn't ultimately answer the question, which was about passing the resulting variable out of the function. I like up-votes, but there seems to be something of a *herd mentality* sometimes here on SO. You didn't give the OP anything more than what was originally posted.
patrick dw
Generally you pass in the initial object. Of course it doesn't have to be an *empty* object - you can, for example, "reduce" into an object from one list, and then do it again (to the same object) from another. Yes, it's almost the same as just using `.each()` and an external variable, but it's more "functional" in that the construct is a pure expression and no messy variable declarations.
Pointy
@Pointy - Thanks for the `reduce()` explanation.
patrick dw
@patrick I understood "pass the array out of the function" to refer only to the array inside the "inside" function; in other words, I thought he was just a little confused on the terminology. I've re-read it and I am still not sure what he means, esp. since he's got the "Ajax" comment *inside* the click handler. He's made no clarifying comments either.
Pointy
+2  A: 

Try with jquery's map function:

datearray = $('.selected').map(function(){
    return $(this).attr('id');
}).get();

// use ajax to send datearray
Sarfraz
The code `= new Array()` in the first line is unnecessary. The second statement replaces the array.
Greg
@Sarfraz - Same comment I gave Pointy. If you don't chain `.get()` at the end of `.map()`, `datearray` will contain a jQuery object instead of just its array.
patrick dw
@Greg, @patrick: Good points guys. Thanks
Sarfraz
+1  A: 

You don't have to pass on the array to the anonymous function because it lives in the same scope.

Koen
This is true. Any function created within another has scope to the containing functions variables. As such after the each runs you will have your data in datearray and you can just pass datearray into your ajax function.
Jonathan Park
A: 

The array should be loaded; you can send it to the server using jQuery.post...

$.post("process.php", datearray, function(dat) {
  alert('Response: ' + dat);
});
Josh Stodola
+1  A: 

Building on the other answers, here is a simplified version:

var datearray = $('selected').map(function() {
  return this.id;
}).get();

The map function gets the id from each element, and the get function returns an array. Within the anonymous function passed to map, this refers to to each selected element in turn.

Greg