Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DIV height for each by it's data-attribute

I am trying to loop through over each div that has a class of block and set it's height individually based on it's data-attribute data-height

HTML

<div class="block" data-height="300"></div>
<div class="block" data-height="500"></div>
<div class="block" data-height="700"></div>

jQuery

$('.block').each(function(){ 
    var size = $this.attr('data-height');
    $(this).height(size);
});

JS Fiddle http://jsfiddle.net/MLnBY/166/

It's not returning the height when I have it in the each method though thus not setting the height for each one.

like image 661
Myoji Avatar asked Apr 29 '26 08:04

Myoji


1 Answers

The problem is in $this; replace it with $(this)

var size = $(this).attr('data-height');
like image 149
nicael Avatar answered Apr 30 '26 20:04

nicael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!