Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ID of all elements of a certain class into an array

Tags:

jquery

Here's what I'm trying to do:

Currently I am using this to create an array of all elements matching the class name of .cookie. Right now I am getting the text value of that element, which is not what I need:

var getAllCookies = $('.cookie').text();
var cookiesArray = jQuery.makeArray(getAllCookies);
alert(cookiesArray[0]);

What I need is to find all elements of a certain class (.cookie), get that element's ID value and store that ID value inside of array.

like image 862
Michael Rader Avatar asked Nov 16 '12 22:11

Michael Rader


1 Answers

I think this should do what you're after:

var ids = $('.cookie').map(function(index) {
    // this callback function will be called once for each matching element
    return this.id; 
});

Documentation for map.

Here's a working jsFiddle demo.

like image 136
John Vinyard Avatar answered Sep 23 '22 11:09

John Vinyard