tags:

views:

51

answers:

3

Ok so I have about six of these calendars on page, so thats around 180 td items (1 for each date):

http://i44.tinypic.com/eiwphl.jpg

each td has an id that is equal to that day's timestamp. the dates I want are the red ones (class of .booked).

So i need the fastest way to 'serialize' the ids for td.booked items, any ideas?

A: 

How about this:

var str = '';

$(function(){
  $('td.booked').each(function(index){
     str += '&td' + index + "=" + encodeURIComponent($(this).text());
  });
});

alert(str);
Sarfraz
Will $('td.booked') selector perform better than $('.booked')?
Raja
@Raja: Yes it will because it is more specific, that is it will look into only in TDs not in all elements.
Sarfraz
derek
@drousseau: Yes, you are right :)
Sarfraz
I think it would be much easier to do `.push({ name: 'td', value: $(this).text() })` and call [`jQuery.param()`](http://api.jquery.com/jQuery.param/) afterwards if this is what you're after.
Nick Craver
@Nick Craver: yes it would, thanks for that.
Sarfraz
+3  A: 

You can get an array of the IDs using .map(), like this:

var ids = $("td.booked").map(function() { return this.id; }).get();

This would result in an array like this:

["id1", "id2", "id3", ... ]
Nick Craver
Wow Nick, you're a bloody legend!
Haroldo
@Nick Craver: +1 for `map` function.
Sarfraz
+2  A: 

What exactly do you mean by "serialize"?

If they are being marked on the client, then the fastest way to collect references to them would be to collect them as they are marked. Then, you already an index to them when you need it, and no retroactive query is needed.

harpo
If you did this, you'd also have to *remove* those that you unmark, meaning more iterations over your collection than just 1 on submit...there's no guarantee this is faster if you can mark *and* unmark selections, which is usually the case.
Nick Craver
That's why more info would be needed to give the best answer. I was assuming that the OP wanted the shortest time between first needing the collection and having it. Obviously this time has to be taken up elsewhere. I agree that map is the best way to collect the id's on-the-spot.
harpo