inserting text on table cell from external js

B

Ben

Hi all,

I'm trying to write inside a table cell from external javascript but
am not successful. When I insert inside a form within <td...>, it
works but does not work for normal table cell. My codes are as
follows; please read comments on the code:

index.html
----------
<html>
<head>
<title>Hello World</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<link href="styles.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript"
src="jatest.js"></script>
</head>

<body onLoad="startclock()">
<center>

<table width="100%" height="431" border="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="148" height="23">&nbsp;</td>
<td width="346">&nbsp;</td>
<td id="date" width="104" valign="top"><div align="right">
/*****************************************************/
/*** I WANT TO CALL date() FR0M EXTERNAL FILE HERE ***/
/*********************** HOW??? **********************/
/*****************************************************/
</div></td>
<td width="148"> <div align="right"> </div></td>
<tr>
<td width="148" height="40">&nbsp;</td>
<td>&nbsp;</td>
<td valign="top"> <div align="right">
<form name="clock">
<input align="right" border="0" type="text" name="face"
size=13 readonly="true">
</form>
</div></td>
<td width="148"> <div align="right"></div></td>
</tr>
</table>

</center>
</body>
</html>


jatest.js
----------
var timerID = null;
var timerRunning = false;

function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}

function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
if (timeValue == "0") timeValue = 12;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " PM" : " AM"
document.clock.face.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}

function startclock() {
stopclock();
showtime();
}

function date() {
var mydate=new Date()
var day=mydate.getDay()
var mydate=new Date()
var year=mydate.getFullYear()
var month=mydate.getMonth()
var daym=mydate.getDate()
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June"
,"July","August","September","October","November","December")
document.date.write(dayarray[day]+", ")
document.date.write(montharray[month]+" "+daym+", "+year)&nbsp
}

Hope some of you can help me...

Thanx
Ben
 
J

Joakim Braun

Ben said:
Hi all,

I'm trying to write inside a table cell from external javascript but
am not successful. When I insert inside a form within <td...>, it
works but does not work for normal table cell. My codes are as
follows; please read comments on the code:
<code snipped>

You'll want to look up DOM (and its limitations browser-wise).

I'm a DOM newbie myself, so this code is probably suboptimal, but the
following works in IE 6 and Netscape 7 for Windows:

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function setCellText(inText){

var theTable = document.getElementById("thing");
var theRows = null;
var theColumns = null;

if(theTable){

theRows = theTable.getElementsByTagName("tr");

if(theRows.length > 0){

// Access columns of first row
theColumns = theRows[0].getElementsByTagName("td");

if(theColumns.length > 0){

// Add a text node to the first column
theColumns[0].appendChild(document.createTextNode(inText));

}
}
}
}
</script>

</head>

<body>
<table id="thing">
<tr><td></td><td></td></tr>
</table>

<form name="form1" >
<input type="button" name="test" value="Try it out"
onclick="setCellText('yadda yadda');">
</form>
</body>
</html>

Joakim Braun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top