javascript - Mark highest and lowest value in HTML table -
i have html table , want mark cells adding class highest , lowest value of each column. have found few related questions here, code misbehaves.
var $table = $("#mytable"); $table.find("th").each(function(columnindex) { var oldvalue=0, currentvalue=0, $elementtomark; var $trs = $table.find("tr"); $trs.each(function(index, element) { oldvalue= currentvalue; var $td = $(this).find("td:eq("+ columnindex +")"); if ($td.length!=0) { currentvalue= parsefloat($td.html()); if(currentvalue > oldvalue) { $elementtomark= $td; } if(index == $trs.length-1) { $elementtomark.addclass("highest"); } } }); }); var $table = $("#mytable"); $table.find("th").each(function(columnindex) { var oldvalue=1000000, currentvalue=1000000, $elementtomark; var $trs = $table.find("tr"); $trs.each(function(index, element) { oldvalue= currentvalue; var $td = $(this).find("td:eq("+ columnindex +")"); if ($td.length!=0) { currentvalue= parsefloat($td.html()); if(currentvalue < oldvalue) { $elementtomark= $td; } if(index == $trs.length-1) { $elementtomark.addclass("lowest"); } } }); });
here jsfiddle: link
the problem doesn't mark right values , cannot see reason.
you updating oldvalue
@ wrong place.
var $table = $("#mytable"); $table.find("th").each(function(columnindex) { var oldvalue=0, currentvalue=0, $elementtomark; var $trs = $table.find("tr"); $trs.each(function(index, element) { var $td = $(this).find("td:eq("+ columnindex +")"); if ($td.length!=0) { currentvalue= parsefloat($td.html()); if(currentvalue > oldvalue) { $elementtomark= $td; oldvalue= currentvalue; } if(index == $trs.length-1) { $elementtomark.addclass("highest"); } } }); }); var $table = $("#mytable"); $table.find("th").each(function(columnindex) { var oldvalue=1000000, currentvalue=1000000, $elementtomark; var $trs = $table.find("tr"); $trs.each(function(index, element) { var $td = $(this).find("td:eq("+ columnindex +")"); if ($td.length!=0) { currentvalue= parsefloat($td.html()); if(currentvalue < oldvalue) { $elementtomark= $td; oldvalue= currentvalue; } if(index == $trs.length-1) { $elementtomark.addclass("lowest"); } } }); })
;
Comments
Post a Comment