Reputation: 279
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.
Upvotes: 3
Views: 2604
Reputation: 131
Hey you can do it using pure javascript like this:
var size = this.getAttribute('data-height');
Upvotes: 1
Reputation: 19005
The problem is in $this
; replace it with $(this)
var size = $(this).attr('data-height');
Upvotes: 5