move the document.write line within the html code

I have a javascript code that works to display the time at the top of the page.  However, I can't seem to figure out how to move it to display exactly where I want it on the page.  Can you tell me what I need to do to have the clock display further down in the page ?  Is there a way to move the "document.write..." line further down ?
var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()
 
  var suffix = "AM";
  if (hours >= 12) {
  suffix = "PM";
  hours = hours - 12;
  }
  if (hours == 0) {
  hours = 12;
  }
  if (minutes < 10)
  minutes = "0" + minutes
  document.write("<b>As Of " + hours + ":" + minutes + " " + suffix + "</b>")

Open in new window

drelingerAsked:
Who is Participating?
 
HonorGodSoftware EngineerCommented:
For example, this works just fine.
<html>
<head>
<script type='text/javascript'>
  function showtime( id ) {
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
 
    var suffix = "AM";
    if ( hours >= 12 ) {
      suffix = "PM";
      hours = hours - 12;
    }
    if (hours == 0) {
      hours = 12;
    }
    if (minutes < 10) {
      minutes = "0" + minutes
    }
    var place = document.getElementById( id )
    if ( place ) {
      place.innerHTML = "<b>As Of " + hours + ":" + minutes + " " + suffix + "</b>"
    } else {
      alert( 'showtime() : specified element not found. id="' + id + '"' )
    }
  }
  window.onload = function() { showtime( 'myTime' ) }
</script>
</head>
<body>
<table border='1'>
  <tr>
    <td><span id='myTime'></span></td>
  </tr>
</table>
</body>
</html>

Open in new window

0
 
HonorGodSoftware EngineerCommented:
The document.write() only works when the script is executed as part of the HTML body.

If you want to have something (e.g., text), displayed on a specific part of the page, you could use a function to display the text as part of a <span>, or <div>

For example:

- create a span like:

<span id='myTime'></span>

- and add this code in the <head> portion of your document
<script type='text/javascript'>
  function showtime( id ) {
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
 
    var suffix = "AM";
    if ( hours >= 12 ) {
      suffix = "PM";
      hours = hours - 12;
    }
    if (hours == 0) {
      hours = 12;
    }
    if (minutes < 10) {
      minutes = "0" + minutes
    }
    var place = document.getElementById( id )
    if ( place ) {
      place.innerHTML = "<b>As Of " + hours + ":" + minutes + " " + suffix + "</b>"
    } else {
      alert( 'showtime() : specified element not found. id="' + id + '"' )
    }
  }
  window.onload = function() { showtime( 'myTime' ) }
</script>

Open in new window

0
 
drelingerAuthor Commented:
Thank you very much.  Worked perfectly.
0
 
HonorGodSoftware EngineerCommented:
Excellent.  I'm glad to have been able to help.

Thanks for the grade & the points.

Good luck & have a great day.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.