Cashton Twoade
Cashton Twoade

Reputation: 11

javascript countdown clock

im trying to write a countdown clock to a certain day in js. I have the following which works if i output it to the page but I cant seem to write it to a div using innerhtml?

Can anybody see where im going wrong?

today = new Date();
expo = new Date("February 05, 2012");
msPerDay = 24 * 60 * 60 * 1000 ;
timeLeft = (expo.getTime() - today.getTime());
e_daysLeft = timeLeft / msPerDay;
daysLeft = Math.floor(e_daysLeft);
document.getElementById('cdown').innerHTML = document.write(daysLeft);

Upvotes: 1

Views: 464

Answers (3)

Karl Nicoll
Karl Nicoll

Reputation: 16419

on this line:

document.getElementById('cdown').innerHTML = document.write(daysLeft);

remove the document.write(. If you're setting the contents of an element, you don't need to write to the document as well.

Fixed code:

today = new Date();
expo = new Date("February 05, 2012");
msPerDay = 24 * 60 * 60 * 1000 ;
timeLeft = (expo.getTime() - today.getTime());
e_daysLeft = timeLeft / msPerDay;
daysLeft = Math.floor(e_daysLeft);
document.getElementById('cdown').innerHTML = daysLeft;

Upvotes: 2

Alex
Alex

Reputation: 9471

Don't use document.write(daysLeft), that will write daysLeft to the document, not the div! Just set daysLeft:

document.getElementById('cdown').innerHTML = daysLeft;

Upvotes: 2

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35126

document.getElementById('cdown').innerHTML = daysLeft

You don't need document.write

Upvotes: 7

Related Questions