jquery - Replacing a text value in a table cell with Javascript -
i struggling mask data value javascript. work @ school, , doing away "traditional" grading. such, don't want gradebook display percentages, instead show words based on percentages (yeah know). replace % value word string. js, correctly alerts me values, return value never replaces % value in cell. missing? extremely new js may simple mistake.
$('.percentage').each(function() { var corrected = $(this).html(); corrected = parsefloat(corrected); alert(corrected); switch (true) { case (corrected < 48.75): return 'unsatisfactory'; break; case (corrected < 61.25): return 'approaching '; break; case (corrected < 73.75): return 'approaching+ '; break; case (corrected < 86.25): return 'meeting '; break; case (corrected < 94.9): return 'meeting+ '; break; case (corrected <= 100): return 'exceeding '; break; default: return 'n/a'; } })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
this do. parses text of cell , replaces string-value based on current score.
$(".score").each(function() { var $this = $(this); $this.text(() => { var num = parsefloat($this.text()); var val; if (num < 48.75) val = 'unsatisfactory'; else if (num < 61.25) val = 'approaching '; else if (num < 73.75) val = 'approaching+'; else if (num < 86.25) val = 'meeting'; else if (num < 94.9) val = 'meeting+'; else if (num <= 100) val = 'exceeding'; return val; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>score</td> <td class="score">48.75</td> </tr> <tr> <td>score</td> <td class="score">38.25</td> </tr> <tr> <td>score</td> <td class="score">85.12</td> </tr> <tr> <td>score</td> <td class="score">67.50</td> </tr> <tr> <td>score</td> <td class="score">87.80</td> </tr> <tr> <td>score</td> <td class="score">10.50</td> </tr> <tr> <td>score</td> <td class="score">96.50</td> </tr> </table>
the class on td makes sure you're parsing cells actual score.
Comments
Post a Comment