Ar attribute of an array of returned matched elements using jQuery

In jQuery, it's easy to select elements as an array.

$ ("a"); // return as elements of an array of anchors

But is it possible to select the attributes of the mapped elements as an array?

Currently I need to do something like ...

links = [];

$ ("a"). each (function () {

href = $(this).attr("href"); links.push(href); 

});

Is there a better way to populate a link variable with href of all matched anchors?

+7
javascript jquery
source share
2 answers

Use $ .map like this:

 var links = $('a').map(function() { return this.href }).get() 
+18
source share
 var links = $("a").map(function(){return $(this).attr("href")}).get(); 
+5
source share

All Articles