is usage from multifiltering with jQuery useful? -
i got code
$('.pbwarn div').filter(function(index){ return parseint(this.innerhtml) > 60; }).addclass("pb_yellow"); $('.pbwarn div').filter(function(index){ return parseint(this.innerhtml) > 80; }).addclass("pb_red");
and want know if useful? or maybe way realize this.
i want change green bar yellow when percentage larger 60 (id="max60"
) , change red if percentage larger 80 (id="max80"
)
just want script running perfect. asking if there potential improvement :d
you can check demo better understanding
i'd suggest:
$('.pbwarn div').addclass(function(){ var num = parseint((this.textcontent || this.innertext),10); if (num > 80) { return 'pb_red'; } else if (num > 60) { return 'pb_yellow'; } });
the anonymous function within addclass()
iterate on elements returned selector, and, in each iteration, $(this)
/this
refer current element; benefit of need call fewer jquery methods (which reduces time spent iterating/reiterating on same set of elements).
references:
Comments
Post a Comment