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?
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?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 28, 2010 at 16:34 HowardHoward 19.8k36 gold badges115 silver badges187 bronze badges2 Answers
Reset to default 19Use $.map like so:
var links = $('a').map(function() { return this.href }).get()
var links = $("a").map(function(){return $(this).attr("href")}).get();