i created html rubric allows user select cell , have add earned points versus points possible. right now, have function allows 1 td
per row selected. when cell selected, adds points variable. issue is, when change selection row, adds new selection on top of variable, doesn't subtract , replace value.
for example, if had 8 point selection made, change 6, instead of variable value being 6, adds 6 8.
the function have add points follows:
jquery('#ldrm-rubric-loaded td.choice').click(function () { // obtain points earned var ndx = jquery(this).index() + 1; var target = jquery('#ldrm-rubric-loaded thead tr.points th:nth-child('+ndx+')').html(); if(!isnan(target) && target.length != 0) { pointsearned += parsefloat(target); } jquery('#ldrm-points-earned').html('points earned: '+pointsearned); alert(pointsearned); });
http://jsfiddle.net/f6u2pjgu/1/
any ideas on how alter function replace value instead of adding on it?
i think easier if use single handler like
var total = 0, $headers = jquery('#ldrm-rubric-loaded thead tr.points th'); jquery('#ldrm-rubric-loaded td.choice').click(function () { var $this = jquery(this), $prev; if (!$this.hasclass('selected')) { $prev = $(this).siblings('.selected').removeclass('selected'); if ($prev.length) { total -= +$headers.eq($prev.index()).html() || 0; } total += +$headers.eq($this.index()).html() || 0; $this.addclass('selected'); jquery(this).siblings().removeclass('selected'); var trackchanges = jquery('#ldrm-rubric-loaded').clone().html(); jquery('#ldrm_assignment_content').val(trackchanges); jquery('#ldrm-points-earned').html('points earned: ' + total); alert(total); } //else don't since selected });
demo: fiddle
if want retain code structure
jquery('#ldrm-rubric-loaded td.choice').click(function () { var $this = jquery(this), $tr = $this.closest('tr'), prevvalue = $tr.data('selected') || 0; // obtain points earned var ndx = $this.index() + 1; var value = +jquery('#ldrm-rubric-loaded thead tr.points th:nth-child(' + ndx + ')').html() || 0; pointsearned -= prevvalue; pointsearned += value; $tr.data('selected', value) jquery('#ldrm-points-earned').html('points earned: ' + pointsearned); alert(pointsearned); });
demo: fiddle
Comments
Post a Comment