populate collection with each()
I am trying to populate a collection using an each. I can get the element in one string, and I can get an alert to fire on each iteration. I just cant put the two together.
Here is my list:
4/27/2011 5:42:54 PM
4/27/2011 5:43:00 PM 4/27/2011 5:46:53 PM 4/28/2011 2:10:40 PMHere is my code.
if (chosenTime > 0) {
var myDateClass = $('.exceptionDate')
var i = 0
$(myDateClass).each(function () {
var myDateClassVal = myDateClass[i].GetAttributeNode(开发者_如何学运维outerText);
//this currently gives an error
//if I switch this to myDateClass.text();
// it returns the list in one string
i++
});
inside the each use $(this).text()
. this
is the individual item within the each.
Try using the map function:
var selected = $('.exceptionDate').map(function(i, e) {
return $(e).text();
});
alert(selected.length);
http://jsfiddle.net/hunter/2wYTT/
I think you are confused about .each()
.
You may want to look at the API:
http://api.jquery.com/each/
$(myDateClass).each(function () {
var myDateClassVal = myDateClass[i].GetAttributeNode(outerText);
i++; // This is not needed.
});
So, if you define it as each(function(indx, elem) { })
it will help you iterate over each element, elem
, or refer to it by index.
I don't entirely understand what you mean, but if you want an Array of the text content of each .exceptionDate
element, you can use .map()
with .get()
.
var array = myDateClass.map(function () {
return $(this).text();
}).get();
Or in a similar manner, you can use jQuery.map()
:
var array = $.map(myDateClass,function (el) {
return $(el).text();
});
精彩评论