Solved

.each running too slowly

Posted on 2013-01-06
1
128 Views
Last Modified: 2013-01-28
Ive built a simple function to look at each div, and then then height of each child div inside, get the maximum height, and then set the largest height to all of the child divs:-
                $(".clsTransactionRow").each( function () {
                  var maxHeight = 0;
                  $(this).find("div").each(function () { maxHeight = Math.max(maxHeight,$(this).height()); });
                  $(this).find("div").height(maxHeight);
                  });

Open in new window


It works fine, however I have 2796 divs that are .clsTransactionRow so the above code take 45 seconds to run, which is unacceptable.

Can anyone think of anyway to optimise the my code, or make it quicker?

Thank you
0
Comment
Question by:tonelm54
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38749032
Could you use a good size with CSS?
Else try this :
  $(".clsTransactionRow").each( function () {    
    var maxHeight = 0;
    var divs = this.getElementsByTagName("div");
    for(var d=0;d<divs.length;d++) {
      var h = $(divs[d]).height();
      if(h>maxHeight) {
        $(divs[d]).prev("div").height(h);
        maxHeight = h;
      }
    };
  });

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question