Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:11:11+00:00 2026-06-13T09:11:11+00:00

I have looked everywhere and I could not find a script that would do

  • 0

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>&nbsp;<i class="icon-minus"></i>&nbsp;</h1></a>
<a href="#" class="btn"><h1>&nbsp;<input type="text" value="0:50" id="input-time">&nbsp;</h1></a>
<a href="#" class="btn" onclick="AddTime('#input-time')"><h1>&nbsp;<i class="icon-plus"></i>&nbsp;</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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:11:13+00:00Added an answer on June 13, 2026 at 9:11 am

    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:

    <a href="#" class="btn btn-subtract">subtract</a>
    <input type="text" value="1:50" id="input-time" data-minutes="110"/>
    <a href="#" class="btn btn-add">add</a>
    

    JS (assumes jQuery 1.8+):

      $(function() {// on document ready
        var $input = $('#input-time');//cache the input as a jQuery object (optional)
        //bind events to the buttons
        $('.btn-add').on('click', function(){
          changeTime(1);
        });
        $('.btn-subtract').on('click', function(){
          changeTime(-1);
        });
    
        function changeTime(mins){
          var currentTime = parseInt($input.data('minutes')), //get the current value from the data attr in the HTML
              newTime = currentTime + mins, //calculate the new time
              minutes = (newTime % 60).toString(), //get the minutes using the modulus operator and convert to a string (so we can use length below)
              hours = (Math.floor(newTime / 60)).toString();// get the hours and convert to a string
    
          //make sure we've got the right length for the minutes string
          if (minutes.length === 0){
            minutes = "00";
          }
          else if(minutes.length === 1){
            minutes = "0" + minutes;
          }
          //update the data attr and the displayed output
          $input.data('minutes', newTime).val(hours + ":" + minutes);
    
        }
      });
    

    (also, check your html structure: you had a h1 around 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:

    //before you bind your events
    $input.data('minutes', parseTimeString($input.val()));
    
    function parseTimeString(str){
      var time = str.split(":");
      return (parseInt(time[0])*60) + parseInt(time[1]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked everywhere and can't find a solution that works for this. I
I have looked everywhere on the internet and can not find this answer. Do
Okay, I have looked everywhere and tried almost every suggestion that I could find
I have looked everywhere for this and just cannot find the answer. I have
I have looked everywhere and surprisingly can't find a good solution to this! I've
I have looked everywhere for the answer to this question, but cannot find anything
I have looked everywhere but have not found a problem similar to this. I
I have looked everywhere to find a linux utility that will allow me to
I have looked everywhere for a simple answer to this but I have not
I have looked everywhere trying to find the answer to this question, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.