Click to See Complete Forum and Search --> : how to reference and change plain text


lcscne
06-23-2004, 02:43 PM
I have an input field:

<form form" action="examFormProc.asp" method="post" name="frmExam">
...
...
...
<input name="examWeight" type="text" size="2" maxlength="3" />
...
...

right next to it I have a table:

<table cellspacing='0' class='weightAppliesTo'>
<tr>
<th>Assignment Grades</th>
<th>+</th>
<th>Exam Grade</th>
<th>=</th>
<th>Quarter Grade</th>
</tr>
<tr>
<td>(100 - document.frmExam.examWeight.value) goes here</td>
<td>+</td>
<td>(document.frmExam.examWeight.value) goes here</td>
<td>=</td>
<td>100%</td>
</tr>
</table>


How do I reference the text in the first and third <td>s in the table in order to change the text?

Vladdy
06-23-2004, 02:51 PM
First correct table structure:
<table cellspacing='0' class='weightAppliesTo' id="myTable">
<thead>
<tr>
<th>Assignment Grades</th>
<th>+</th>
<th>Exam Grade</th>
<th>=</th>
<th>Quarter Grade</th>
</tr>
</thead>
<tbody>
<tr>
<td>(100 - document.frmExam.examWeight.value) goes here</td>
<td>+</td>
<td>(document.frmExam.examWeight.value) goes here</td>
<td>=</td>
<td>100%</td>
</tr>
</tbody>
</table>
Assuming you have only one row:

//Array of body cells
var cells = document.getElementById("myTable").tbody.getElementsByTagName('td');
//First cell value
cells[0].firstChild.nodeValue = 'your value';

lcscne
06-24-2004, 06:36 AM
thanks Vladdy,
I had to remove the ref to tbody as I recieved tbody is null or not an object. So ended up with this:

function setTableData(examWeight) {
var oTable = document.getElementById("examWeightTable");
var aCells = oTable.getElementsByTagName('td');
aCells[0].firstChild.nodeValue = 100 - examWeight;
}


I don't need two variables but it made it easier for me to determine the tbody error. Thanks man, as always your time and effort here is much appreciated.