Reputation: 11
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
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
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
Reputation: 35126
document.getElementById('cdown').innerHTML = daysLeft
You don't need document.write
Upvotes: 7