Each element in $(some_selector)
has attribute my_attr
(which is a number).
I would like insert all these attributes to array.
What would be the easiest way to do this using jQuery ?
Each element in $(some_selector)
has attribute my_attr
(which is a number).
I would like insert all these attributes to array.
What would be the easiest way to do this using jQuery ?
You can use .map()
for this:
var arr = $("some_selector").map(function() {
return $(this).attr("my_attr");
}).get();
Or as a number, parse along the way:
var arr = $("some_selector").map(function() {
return parseInt($(this).attr("my_attr"), 10);
}).get();
Either of these return a JavaScript Array.