each function in javascript for new elements -
i have page infinite scroll appends new elements user scrolls down. ".product-variation" 1 such element. want remove first option of newly arriving elements. code below job think goes , down dom bit - retarding performance in process. there better , more efficient way of removing first option of newly arriving elements?
the first option of newly arriving element '' , that's trying remove.
jq(".product-variations").each(function(){ var fresh_variant = jq(this).find("option:nth-child(1)").val(); if(fresh_variant == '') { jq(this).find("option:nth-child(1)").remove(); } jq(this).trigger('change'); });
i'd put flag :
jq(".product-variations:not([data-set])").each(function(){ var fresh_variant = jq(this).find("option:nth-child(1)").val(); if(fresh_variant == '') { jq(this).find("option:nth-child(1)").remove(); } jq(this).trigger('change'); jq(this).attr('data-set',1); });
so , when add item , set flag "set".
next time , scan new ones.(who don't have flag)
oh , , btw here better version of code :
jq(".product-variations:not([data-set])").each(function(){ var $this=jq(this); var $opt=$this.find("option:nth-child(1)"); var fresh_variant = $opt.val(); if(fresh_variant == '') { $opt.remove(); } $this.trigger('change'); $this.attr('data-set','whatever'); });
Comments
Post a Comment