I have a problem with my calculation answer.
What ever is entered in the text input for each answer in a
question, it will subtract the number under the “Total Marks” column
for that question with the numbers entered within the text input for
that question.
Example is below:

In the text inputs I entered in numbers 1, 1 and 2. The Total Narks under the Total Marks Remaining column was 5 but it is now 1 as 5 – 1 – 1 – 2 = 1 Total Mark Remaining
The problem I have though is that if I change the top text input to lets say 3, then the calculation should now be 5 – 3 – 1 – 2 = -1 Total Mark Remaining. But it doesn’t do this as that somewhere in the jquery function it has stated that if Total Marks Remaining number is less than 0, then display the original Total Marks Remaining number which is 5. This is incorrect, it should just display the minus number so the user knows that they need to reduce 1 mark. How can I get the minus number to be displayed if the Total Marks Remaining number is less than 0?
Below is the code for the jquery function:
Jquery:
$(function () {
//alert("here");
var questions = $('#markstbl td[class*="_ans"]').length - 1;
//disable single entry
for (var i = 0; i <= questions; i++) {
if ($("[class*=q" + i + "_mark]").length == 1) {
var t_marks = $("[class*=q" + i + "_ans]").html();
//alert(t_marks);
$("[class*=q" + i + "_mark]").val(t_marks)
.attr("disabled", "disabled");
//$("[class*=q"+i+"_mark]").attr("disabled","disabled");
}
}
//find each question set and add listeners
for (var i = 0; i <= questions; i++) {
$('input[class*="q' + i + '"]').keyup(function () {
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q') + 1, cl.indexOf('_'));
var tot_marks = $(".q" + questionno + "_ans_org").val();
//alert(tot_marks);
var ans_t = 0;
$("[class*=q" + questionno + "_mark]").each(function () {
var num = (isNaN(parseInt($(this).val()))) ? 0 : parseInt($(this).val());
ans_t += parseInt(num);
});
ans_t = tot_marks - ans_t;
//alert(ans_t);
//var fixedno = tot_marks;
var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t;
$(".q" + questionno + "_ans").val(ans);
$(".q" + questionno + "_ans_text").html(ans);
});
}
});
Below is the dynamic HTML Table:
HTML:
<body>
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='noofmarksth'>Total Marks Remaining</th>
</tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionId);
$prev_ques = '';
foreach ($searchQuestionId as $key => $questionId) {
?>
<tr class="questiontd">
<?php
if ($questionId != $prev_ques) {
?>
<td class="questionnumtd" name="numQuestion" rowspan=
"<?php echo $row_span[$questionId]; ?>"><?php
echo $questionId;
?><input type="hidden" name="q<?php echo $questionId; ?>_ans_org" class=
"q<?php echo $questionId; ?>_ans_org" value=
"<?php echo $searchMarks[$key]; ?>" /><input type="hidden" name=
"q<?php echo $questionId; ?>_ans" class=
"q<?php echo $questionId; ?>_ans" value=
"<?php echo $searchMarks[$key]; ?>" /></td>
<td class="questioncontenttd" rowspan="<?php echo $row_span[$questionId]; ?>">
<?php
echo $searchQuestionContent[$key];
?></td><?php
}
?>
<td class="answertd" name="answers[]"><?php
echo $searchAnswer[$key];
?></td>
<td class="answermarkstd"><input class=
"individualMarks q<?php echo $questionId; ?>_mark_0" q_group="1" name=
"answerMarks[]" id="individualtext" type="text" /></td><?php
if ($questionId != $prev_ques) {
?>
<td class="noofmarkstd q<?php echo $questionId; ?>_ans_text" q_group="1"
rowspan="<?php echo $row_span[$questionId]; ?>"><?php
echo $searchMarks[$key];
?></td><?php
}
?>
</tr><?php
$prev_ques = $questionId;
}
?>
</tbody>
</table>
</body>
</html>
This line is the culprit:
Change it to: