PDA

View Full Version : javascript value to table


ngaisteve1
01-29-2003, 01:44 AM
Is it possible to put a value generated by a javascript function to a table below? The case is related to the rate calculator last post. After I calculate a rate, I hope to have the result generated in the same page.

<table border=1 cellspacing=0 cellpadding=3>
<tr>
<td>No</td>
<td>Country</td>
<td>Type</td>
<td>Weight</td>
<td>Total</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

kdjoergensen
01-29-2003, 09:49 AM
<table border=1 cellspacing=0 cellpadding=3>
<tr>
<td>No</td>
<td>Country</td>
<td>Type</td>
<td>Weight</td>
<td>Total</td>
</tr>
<tr>
<td id="no_output"></td>
<td id="country_output"></td>
<td id="type_output"></td>
<td id="weight_output"></td>
<td id="total_output"></td>
</tr>
</table>

<script language="javascript">

if (document.all){
document.all["no_output"].innerHTML = "<B>45</B>";
} elseif (document.getElementById){
document.getElementById("no_output").innerHTML = "<B>45</B>";
}
</script>

If you need it to work in Netscape4 too then use following (just one table cell example):

<td id="no_output">
<ilayer name="NS_no_output">
<layer name="NS1_no_ouput">
</layer>
</ilayer>
</td>

if (document.layers){
var nstemplayer = document.layers["NS_no_output"].document.layers["NS1_no_output"];
nstemplayer.document.open();
nstemplayer.document.write("<B>45</B>");
nstemplayer.document.close();
}

MSIE/Netscape 6/7 will ignore the layer/ilayer tags.

ngaisteve1
01-29-2003, 08:41 PM
Originally posted by kdjoergensen
<script language="javascript">

if (document.all){
document.all["no_output"].innerHTML = "<B>45</B>";
} elseif (document.getElementById){
document.getElementById("no_output").innerHTML = "<B>45</B>";
}
</script>


kdjoergensen, can u describe a bit this javascript how it works? Thanks.

kdjoergensen
02-03-2003, 09:38 AM
In short:

Take the element with the ID named no_output and insert some date inside this element and in the process convert it/display it as if it was HTML

example:

<B id="mybold">HI</b>
Result: HI

document.all["mybold"].innerHTML = "<I>HI again</I>";

It will insert the string "<I>HI again</I>" inside the element named 'mybold', e.g.:
<B Id="mybold"><I>HI again</I></B>

Result: HI again

MSIE browsers use 'all' to capture elements by id. DOM capable browsers (incl IE5+6/NS6+7) uses getElementById to capture elements by id.