By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,521 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

inserting text on table cell from external js

P: n/a
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","Thu rsday",
"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
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Ben" <cr*********@yahoo.com> skrev i meddelandet
news:d9**************************@posting.google.c om...
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.