attribute values into an array
Hi I'm new to jQuery but loving its power. Anyway, I'm interested in discussion of the following problem:
I have a collection of divs each with their own numerical id that i use for ordering. They look like something like this:
- <div class = "note" noteId = "1">...
- <div class = "note" noteId = "4">...
- <div class = "note" noteId ="23">...
- ...etc
I want to read the noteId values into a javascript array. I have found two successful ways of doing this but I arrived at these solutions by trial and error and I'm curious as to what the accepted way of doing this is.
Here are the successful methods I have used. Method 1 run seems to run marginally faster:
method 1
- arrayVariable =[ ];
- arrayVariable = $("note").map(function()
- {
return $(this).attr("noteId");
}) ;
method 2
- var arrayVariable = [ ];
- $("note").each(function (i)
- {
arrayVariable[i]=$(this).attr("noteId");
});
I'm not satisfied with either of these and am sure there must be a snappy way involving a jQuery function that returns an array of attribute values directly. Something like this, which DOES NOT WORK
- var arrayVariable = [ ];
- arrayVariable = $.makeArray($("note"]).attr("noteId"));
This just returns an empty array. If there isn't a standard way of doing this then which of the two working methods strikes you as the most satisfactory way to achieve this. Thanks for your time.