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".
    – Mitya
    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
    – Vaibs_Cool
    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.
    – Barmar
    Commented Oct 5, 2013 at 11:15
  • "Doesn't work is doesn't work". Don't ask with your uppity.
    – Hai Tien
    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

0
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
  • 1
    use this only when dealing with raw javascipt otherwise Jquery html method is best option.
    – PSK
    Commented Oct 5, 2013 at 11:26
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
  • 1
    This is with jQuery, wich is not tagged in the post... Good to name that in your answer.
    – Sergio
    Commented Oct 5, 2013 at 11:25
  • 1
    It's not difficult to convert on java script @Sergio Commented Oct 5, 2013 at 11:26
  • @Subhash, do that then. Or write somewhere you have jQuery.
    – Sergio
    Commented Oct 5, 2013 at 11:27
  • I write it in javascript Commented Oct 5, 2013 at 11:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.