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?
Use $.map like so:
var links = $('a').map(function() { return this.href }).get()
var links = $("a").map(function(){return $(this).attr("href")}).get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With