I have looked everywhere and I could not find a script that would do this. Anyway I have two buttons +/- and they should either add or subtract minutes that are in a input field. I tried to do it with jQuery but it does not work right.. If somebody could point me into the right direction.
HMTL:
<a href="#" class="btn" onclick="SubTime('#input-time')"><h1> <i class="icon-minus"></i> </h1></a>
<a href="#" class="btn"><h1> <input type="text" value="0:50" id="input-time"> </h1></a>
<a href="#" class="btn" onclick="AddTime('#input-time')"><h1> <i class="icon-plus"></i> </h1></a>
jQuery (added only the function that should add time):
function AddTime(where) {
var oldTime = $(where).val();
var contains = oldTime.split(":");
var hours = parseInt(contains[0], 10);
var minutes = parseInt(contains[1], 10);
var j = 1;
if (minutes == 0) {
return false;
}
if (minutes > 0 && minutes < 11) {
var value = minutes + j;
value = hours + ':0' + value;
$(where).val(value);
}
if (minutes > 10) {
var value = minutes + j;
value = hours + ':' + value;
$(where).val(value);
}
if (minutes > 58) {
hours = hours + 1;
var value = hours + ':0' + j;
$(where).val(value);
}
}
It does not add the hours right, right now the input field value is 0:50 so when I start pushing + button it adds one minute at a time as long as the time is 1:01 and then the hour goes back to 0:02. I don’t know how to add the hours right, I’ll bet there is an easier way of doing this, so please help.
So first things first, you shouldn’t really be using inline event handlers to bind events to dom elements, use jQuery
on()instead. Secondly, in a case like this it’s much easier to track minutes in your logic and then format that value into hours and minutes for display:html:
JS (assumes jQuery 1.8+):
(also, check your html structure: you had a
h1around your input element, which is a bit odd, and a linka round that too. is that really what you meant to do?)Update
If you aren’t able to set a data attribute with the minutes in your html, you can set it with jQuery like this: