2

I know how to get a current date. but I want it to display after tag using document.write function (well. it IS the demand :)/ )

I am trying " <td>document.write(day)<td> " in the table created in form and /form tags, day is the variable i defined in the <script> and it has the correct value;

but it doesn't show the correct info. Can someone tell me how to do that? (HAVE to use document.write function and DO NOT use this function to create a table tag like <td> or <tr>)

dragon66
  • 2,475
  • 2
  • 17
  • 39
user1177245
  • 119
  • 1
  • 3
  • 7

1 Answers1

3

You need to wrap the javascript in the <script> tag.

<td><script>document.write(day);</script><td>

Alternatively, you could assign the td an id and then reference it later.

<td id="writeDay"></td>
...
<script>document.getElementById("writeDay").innerHTML = day;</script>

Or you could even make the whole element and then place it when you want

<td id="writeDay"></td>
<script>
 var dayElement = document.createElement("div");
 dayElement.innerHTML = day;
 function appendDay(target){
  document.getElementById(target).appendChild(dayElement);
 }
</script>
...
<script>appendDay("writeDay");</script>
Travis J
  • 77,009
  • 39
  • 185
  • 250
  • assigning the id and writing to the DOM is a better option. See this question and answer: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Cfreak Jun 12 '12 at 23:17
  • @user1177245 - The script tag will fire up the javascript interpreter. Without it any scripting text used will be just text. – Travis J Jun 12 '12 at 23:18