The closest jQuery behavior in IE is very slow

I have a table with 5000 rows. In each line, I have an html element. myElementList is a list of these items. Now I need to select all tr ​​of these elements. I am using the following code.

myElementList.closest('tr'); 

This works great in FF. But when I run the same in IE 8. The browser freezes and a pop-up message appears saying that propmt is for stopping the script.

Any suggestion why I see this behavior, or its alternative.

Edit

The behavior remains the same when I use parent ()

  myElementList.parents('tr'); 
+4
source share
2 answers

To get what you want pretty quickly (the closest parent tr to each checked box), you could do something like this:

 $.fn.extend({ closestByTagName: function(tagname) { var tag = tagname.toUpperCase(), i = this.length, node, found=[], trParents; while(i--) { node = this[i]; while((node=node.parentNode) && node.nodeName != tag); if(node) { found[found.length] = node; } } return $(found); } }); var result = $('input:checked').closestByTagName('tr'); 

This is ugly, but I can’t come up with a faster way. (it should far surpass jQuery)

+3
source

Try using selectors parent (), child (), next (), prev (). I'm not 100% about exactly how jQuery crosses a table to find tr, but 5,000 rows is a lot. More specific, the js engine will do less work.

http://api.jquery.com/category/traversing/

 $('input:checked').parent('tr').each(function(i){.... 
+1
source

All Articles