Q:

How to perform a calculation function to calculate the difference?

In my application I have a table which looks like this below for example:
Question No.    Question                 Answer     Marks per Answer    Total Marks
1               Here are 2 answers       B          (text input)            5
                                         D          (text input)
2               Here is a single answer  True       (text input)            5

In the table above I have a text input which belongs to each answer. Now each question has its own amount of total marks. What I want to do is that if the user enters in a number in the text input, it should do a calculation on the difference between the number entered in the text input and the number under the "Total Marks" column. So if you look at the example below:
 Question No.   Question                 Answer     Marks per Answer    Total Marks
 1              Here are 2 answers       B          (text input) = 2        2
                                         D          (text input) = 1
 2              Here is a single answer  True       (text input) = 5        0

As you can see in the table above, the text inputs for the answers in question 1 equals 3 altogether. So 5 (from total marks for question 1) minus 3 = 2 (Total marks now equals 2)
For question 2, the text input for the answer in question 2  equals 5, so 5 (from total marks for question 2) minus 5 = 0 (Total marks now equals 0).
MY QUESTIONS:

How and what is the best method to use in order to perform these calculations? 

2.What I also want is that if a question only contains a single answer (like question 2 in the above example only has one answer), then I want the text input to become readonly and the text input should display the same number as the total marks as that it has to equal the same marks anyway, so it would look like this below:
Question No.   Question                  Answer     Marks per Answer             Total Marks
 2              Here is a single answer  True       (text input(readonly)) = 5        0

Here is a jsfiddle demo so you know what the HTML code looks like. In the jsffidle what I have done is that in the textboxes I inserted the values in them, but I left the "Total marks" for questions to "5" when really they should both make "0" if you calculate the difference between the total marks and the numbers entered in the text inputs. Also question 2's text input is readonly, this is because it is just a single answer to this question
Below is the html code which is in the jsfiddle:
<table border='1' id='markstbl'>
<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</th>
</tr>
<tr class="questiontd">
<td class="questionnumtd" rowspan="2">1</td>
<td class="questioncontenttd" rowspan="2">Here are 2 Answers Question</td>
<td class="answertd">B</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="3" /></td>
<td class="noofmarkstd" rowspan="2">5</td>
</tr>
<tr class="questiontd">
<td class="answertd">D</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="2" /></td>
</tr>
<tr class="questiontd">
<td class="questionnumtd" rowspan="2">2</td>
<td class="questioncontenttd" rowspan="2">Here is single Answer Question</td>
<td class="answertd">True</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="5" readonly="readonly"/></td>
<td class="noofmarkstd" rowspan="2">5</td>
</tr>
</table>

Let me just say that there are can be numerous questions and numerous answers, please don't just provide an answer that fits for only this example. It needs to work if there are numerous answers in a question.

A:

Here's a solution that is the basic framework for doing the calcs. You will need to adjust if you need any validation rules
$('tr').each(function() {
    var $input = $(this).find('input');
    var $row = $(this);
    var is_multiple = !$input.prop('readonly');
    var rowClass = is_multiple ? 'multiple' : 'single';
    if (is_multiple) {
        var is_first = $row.find('td').length == 5;
        rowClass += is_first ? ' first' : ' second';
    } else {
        /* readonly just needs marks changed once on page load */
        $row.find('.noofmarkstd').text(5 - $input.val());
    }
    $input.addClass(rowClass);
});

$('input.multiple').keyup(function() {
    var $input = $(this);
    var is_first = $input.is('.first');
    var $row = $input.closest('tr');
    var $otherRow = $row[is_first ? 'next' : 'prev']();
    var $marks = is_first ? $row.find('.noofmarkstd') : $otherRow.find('.noofmarkstd');

    var calcs = 5 - ($input.val() || 0) - ( $otherRow.find('input.multiple').val() || 0);
    $marks.text(calcs);    
});

/* if need calcs for multiples generated on pageload trigger a change on the first in set*/
 $('input.first').change();

DEMO: http://jsfiddle.net/jTXy5/1/

