Get the attribute value of each element from a jQuery set, into an array

How can I get all attributes (e.g. href) of all elements matching a jQuery selector?


Something like this perhaps?

var ids = [];

$('.myClass').each(function () {
  ids.push($(this).attr('id')); // ids.push( would work as well.

Something like

var idArray = $(".someClass").map(function(){

Working demo

One-liner with ES6 arrow functions, using jQuery .map():

const ids = $('a.someClass').map((i, el) => el.getAttribute('href')).get();

<script src=""></script>
<a href="test1" class="someClass">t1</a>
<a href="test2" class="someClass">t2</a>
<a href="test3" class="someClass">t3</a>

More simple solution with Underscore.js

For example: Get all links text who's parents have class someClass

_.pluck($('.someClass').find('a'), 'text');

Working fiddle

Need Your Help

How to set border color of certain Tkinter widgets?

python colors tkinter tcl border

I'm trying to change the background color of my Tkinter app, but for certain widgets it leaves a white border around the edges.

Verify database connection with pg-promise when starting an app

node.js postgresql express pg-promise

I am building an express application that connects to a postgres database using the pg-promise module.