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

后端 未结 3 710
南笙
南笙 2021-01-12 07:45

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

3条回答
  •  广开言路
    2021-01-12 08:19

    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.

提交回复
热议问题