1

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?

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

@SoYuJe I updated my answer. It is storing now on array arr
I thought there will be a shorter way to initialize into an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.