Misha Moroshko
Misha Moroshko

Reputation: 171341

How do I insert attributes into an array?

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 ?

Upvotes: 2

Views: 190

Answers (1)

Nick Craver
Nick Craver

Reputation: 630409

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.

Upvotes: 5

Related Questions