addEventListener using a button doesn't do anything
Playing around with some Javascript and I'm trying to display the clock when a button is clicked. Using just the js without addEventListener
works, it displays the clock in the target div. When I add addEventListener
though, nothing happens. I'm quite new to programming and often overlook some typos and syntax errors but I've been through this many times and didn't find anything.
This is the js code:
document.addEventListener('DOMContentReady', function () {
document.getElementById('clock-button').addEventListener('click', startClock, false);
});
function startClock() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('clock').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function () {
startClock();
}, 500);
console.log("works");
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
And the jsfiddle here: http://jsfiddle.net/Em46R/1/
Replace DOMContentReady with DOMContentLoaded