SoYuJe
SoYuJe

Reputation: 105

jQuery get data into an array

I got few elements with data-replyid, and it is with class post.

By using $('.post') I will get an array of the elements.

By using $('.post').data('replyid') I will get only the data from the first elements.

How can I get all the data-replyid into an array?

Upvotes: 1

Views: 37

Answers (1)

Eddie
Eddie

Reputation: 26854

You can loop and get each element with class post by:

var arr = [];

$( ".post" ).each(function(){
	var replyid = $( this ).data('replyid');
        arr.push( replyid  );
});

console.log( arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="post" data-replyid="1"> </div>
<div class="post" data-replyid="2"> </div>
<div class="post" data-replyid="3"> </div>
<div class="post" data-replyid="4"> </div>

Upvotes: 1

Related Questions