inserting text on table cell from external js

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    #1

    inserting text on table cell from external js

    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.cs s" rel="stylesheet " type="text/css">
    <script language="JavaS cript" type="text/javascript"
    src="jatest.js" ></script>
    </head>

    <body onLoad="startcl ock()">
    <center>

    <table width="100%" height="431" border="0" cellspacing="0" >
    <!--DWLayoutTable-->
    <tr>
    <td width="148" height="23">&nb sp;</td>
    <td width="346">&nb sp;</td>
    <td id="date" width="104" valign="top"><d iv 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">&nb sp;</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(ti merID);
    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("sho wtime()",1000);
    timerRunning = true;
    }

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

    function date() {
    var mydate=new Date()
    var day=mydate.getD ay()
    var mydate=new Date()
    var year=mydate.get FullYear()
    var month=mydate.ge tMonth()
    var daym=mydate.get Date()
    var dayarray=new Array("Sunday", "Monday","Tuesd ay","Wednesday" ,"Thursday",
    "Friday","Satur day")
    var montharray=new Array("January" ,"February","Ma rch","April","M ay","June"
    ,"July","August ","September"," October","Novem ber","December" )
    document.date.w rite(dayarray[day]+", ")
    document.date.w rite(montharray[month]+" "+daym+", "+year)&nbs p
    }

    Hope some of you can help me...

    Thanx
    Ben
  • Joakim Braun

    #2
    Re: inserting text on table cell from external js

    "Ben" <crescent_au@ya hoo.com> skrev i meddelandet
    news:d99e1341.0 407192055.10d40 baa@posting.goo gle.com...[color=blue]
    > 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:[/color]
    <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(inT ext){

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

    if(theTable){

    theRows = theTable.getEle mentsByTagName( "tr");

    if(theRows.leng th > 0){

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

    if(theColumns.l ength > 0){

    // Add a text node to the first column
    theColumns[0].appendChild(do cument.createTe xtNode(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="setCel lText('yadda yadda');">
    </form>
    </body>
    </html>

    Joakim Braun


    Comment

    Working...