user5025242
user5025242

Reputation:

How to Increment the minutes value in timer using javaScript

For a timer(minutes:seconds)if it has time of 30:00 how to increase the minutes part of it and display it in a input box?

My Html Code is

<input name="timer" id="timer" value="30:00" type="text" onclick="incre()"></input>

My javascript code is

function incre()
{
    var textbox = document.getElementById("timer");
    var time = document.getElementById("timer").value.split(":");
    var minutes = parseInt(time[0]);
    var seconds = parseInt(time[1]);
    minutes = minutes++;
}

Upvotes: 0

Views: 401

Answers (2)

LostMyGlasses
LostMyGlasses

Reputation: 3144

Try this:

function incre() {
    var textbox = document.getElementById("timer");
    var time = textbox.value.split(":");
    var minutes = parseInt(time[0]);
    var seconds = parseInt(time[1]);
    minutes++;
    textbox.value = minutes + ':' + seconds;
}

Note that minutes = minutes++ is unnecessary, you just need to do minutes++.

With the last line, you update the input's value.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You are increasing minutes, alright. But you are not updating it in the text field. Also, read the basics. There's no </input>.

  • You don't need a parseInt() for 0.
  • You don't need to assign minutes++ to minutes.

function incre() {
  var textbox = document.getElementById("timer");
  var time = document.getElementById("timer").value.split(":");
  var minutes = parseInt(time[0]);
  var seconds = (time[1]);
  minutes++;
  document.getElementById("timer").value = minutes + ":" + seconds;
}
<input name="timer" id="timer" value="30:00" type="text" onclick="incre()" />

Upvotes: 1

Related Questions