javascript - Run each() function in order of number of parents -
i need run function once every element matches .myclass
- needs run in order of how many parents each element has.
$('.myclass').each(function(index){ //my code });
the thing can think of doing doing each()
function before 1 appends data-sort attribute based on parents().length()
surely there's better way that?
map
number of parents along element array of objects. sort , iterate result.
var els = $('.myclass'); var data = els.map(function(index, el){ return {el: el, pars:$(el).parents().length}; }).toarray() .sort(function(a,b) { return a.pars - b.pars; }); $.each(data, function(i, obj) { var el = obj.el; // el });
Comments
Post a Comment