how to loop div class in jquery

This article is about looping div class using jquery with some examples like adding inline css , adding class to the div, changing div text and collect all data-ids associated with div.

Html Code:
<div data-id='0021' class='clone'>Test1</div>
<div data-id='0022' class='clone'>Test2</div>
Jquery Code:
 
Adding inline css
$.each($('.clone'), function (index, val) {
$(val).css({'color':'black'});
});
Jquery Code:
 
Adding class to the div
$.each($('.clone'), function (index, val) {
$(val).addClass('new_class');
});
Jquery Code:
 
changing div text
$.each($('.clone'), function (index, val) {
$(val).text('Hello'+index);
});
Jquery Code:
 
Collecting data-ids of div and pushing it to array clone
clone = []
$
.each($('.clone'), function (index, val) {
clone.push($(val).data('id'));
});