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';     }         }); 

js fiddle demo.

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

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -