Reputation: 2787
So if I were dealing with just one element, I could use the following:
$("#id").attr( "data-height" , $("#id").css("height") );
But is there any way to do this with every element in a class?
Upvotes: 0
Views: 81
Reputation: 1679
$('.className').children().each(function(i, obj) {
$(obj).attr('data-height', $(obj).css('height'));
});
Upvotes: 0
Reputation: 6260
$('.myElem').each(function() {
$this = $(this);
$this.attr('data-height', $this.height());
});
You could also just use the .data()
method
$('.myElem').each(function() {
$this = $(this);
$this.data('height', $this.height());
});
Upvotes: 2