Below is an example of a table I have displayed in my application:
Marks Per Answer Total Marks Marks Remaining
(blank text input)
(blank text input) 6 6
(blank readonly text input) 4 0
The problem I have is that what is supose to happen is that any readonly text inputs in the table should display the same value as the number under "Total Marks" column. So the table should look like this below:
Marks Per Answer Total Marks Marks Remaining
(blank text input)
(blank text input) 6 6
(readonly text input value='4') 4 0
My question is that what do I need to change in my code below in order to display the correct values in the read only text inputs?
if ($("[class*=q" + i + "_mark]").length == 1) {
//alert(t_marks);
var t_marks = $("[class*=q" + i + "_ans]");
var t_marksHtml = t_marks.html();
t_marks.html("0");
$("[class*=q" + i + "_mark]").val(t_marksHtml).attr('readonly', true);
//$("[class*=q"+i+"_mark]").attr('readonly',true);
}
Below is the HTML:
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="2">6</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="2"><strong>5</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
</tr>
<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="1">6</td>
<td class="noofmarkstd q2_ans_text" q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
</tbody>
</table>
idshould be unique, so you should be usingidfor theqx_mark_0instead of making that a style.$('#markstbl input[readonly]').val(function(i, oldVal) {return $(this).closest("tr").find(".totalmarkstd").text(); });will find any fields that are already readonly and set their value to the total marks value from the same table row...