help with each()

+2 jamie y · November 26, 2014
I have many divs on my page that have a class of "height". I am trying to get the height of all of them so that if the height is greater than 400, it will wrap a div with class scroll. Currently, I can only get it to work on one page. I figured I needed a loop but I am unable to figure it out. This is my current iteration of the code which does seem to work on at least one page



$('div.height').each(function() {
            
if($(this).height() > 400){
$(this).wrap("<div class='scroll'></div>");
} else {
$(this).wrap("<div class='noscroll'></div>");
}
});



Post a Reply

Replies

Oldest  Newest  Rating
0 Samuel Oloruntoba · November 29, 2014
you need to include the code on every page, and also try this, it would save you intepretation time


(function($) {
  var divs = $('div.height');
  
  divs.each(function(index) {
    var div = $(this);
    
    if( div.height() > 400) {
      div.wrap("<div class='scroll'></div>");
    } else {
      div.wrap("<div class='noscroll'></div>");
    }
  });
})(jQuery);
  • 1

Javascript

107,015 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators