7

I want use javascript to append clock into inside of a div. Here is my code :

<script type="text/javascript">
//<![CDATA[
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}

var months = new makeArray('January','February','March','April','May',
'June','July','August','September','October','November','December');
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;

var timer = document.write(months[month]+ " " +day + ", " + year);
document.getElementById('time').appendChild(timer);
//]]></script>

<div id='time'></div>

But it doesn't work. Help me for fixing it. Thanks you.

5
  • 1
    Define "doesn't work". Commented Oct 5, 2013 at 11:13
  • 1
    Working fine jsfiddle.net/5Khyc Commented Oct 5, 2013 at 11:15
  • copy paste the error if any Commented Oct 5, 2013 at 11:15
  • 2
    Learn how to use DOM functions, don't use document.write(). It overwrites the page rather than inserting into it. Commented Oct 5, 2013 at 11:15
  • "Doesn't work is doesn't work". Don't ask with your uppity. Commented Oct 5, 2013 at 11:15

3 Answers 3

7

Try this:

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var day = date.getDate();
var month = date.getMonth();
var yy = date.getFullYear();
var year = (yy < 100) ? yy + 1900 : yy;

var timer = document.createElement('div');
timer.innerHTML = months[month] + " " + day + ", " + year;
document.getElementById('time').appendChild(timer);

Demo here

Sign up to request clarification or add additional context in comments.

Comments

3

innerHTML can write text into the div as you are not appending any element here.

document.getElementById('time').innerHTML = months[month]+ " " +day + ", " + year;

for refernece :http://jsfiddle.net/SpgMm/2/

if you really want to use appendChild better use createElement and then append it to div.

Hope it helps

P.S. use this only when dealing with raw javascipt otherwise Jquery html method is best option.

1 Comment

use this only when dealing with raw javascipt otherwise Jquery html method is best option.
1

Try this, this will help you

var months = new Array('January','February','March','April','May',
'June','July','August','September','October','November','December');
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var timer = months[month]+ " " +day + ", " + year;
$('#time').html(timer);

Or in Javascript you can write as

document.getElementById('time').innerHTML=timer;

Fiddle Here

4 Comments

This is with jQuery, wich is not tagged in the post... Good to name that in your answer.
It's not difficult to convert on java script @Sergio
@Subhash, do that then. Or write somewhere you have jQuery.
I write it in javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.