I have made a reverse timer using java script. I added button for start and reset separately but need to change the button functionality on click. If I click on start then start functionality should run and at the place of start reset button should come and after click on reset again reset functionality should get start and start button should come. How can I do this. Please suggest.
Here is the code
var interval;
var minutes = 1;
var seconds = 00;
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
alert(el.innerHTML = "countdown's over!");
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
var renew = document.getElementById('renew');
renew.onclick = function() {
minutes = 1;
seconds = 00;
clearInterval(interval);
interval = null;
countdown('countdown');
}
var start = document.getElementById('start');
start.onclick = function() {
document.getElementById('renew').onclick = function (event){
<input type="button" value="renew" id="renew" />
};
if (!interval) {
countdown('countdown');
}
}
html code is:
<div id="topmain">
<div style="margin-left:15px; float:left;">
<input type="button" value="start" id="start" />
<input type="button" value="renew" id="renew" />
<div id='countdown' style="color:#0FC; margin-left:5px; " >
</div> </div>