Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return matched elements' attribute ar array using jQuery

In jQuery, it is easy to select elements as array.

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

But is it possible to select matched elements' attributes as array?

Currently I need to do something like...

links = [ ];

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

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

});

Are there any better method to fill the variable links with href of the all matched anchors?

like image 976
Howard Avatar asked Jul 28 '10 16:07

Howard


2 Answers

Use $.map like so:

var links = $('a').map(function() { return this.href }).get()
like image 55
meder omuraliev Avatar answered Oct 21 '22 10:10

meder omuraliev


var links = $("a").map(function(){return $(this).attr("href")}).get();
like image 21
nicholasklick Avatar answered Oct 21 '22 11:10

nicholasklick