Error in script of adding value to integer

I'm probably missing something very easy..  The value of the text box is not to exceed 59 and once it reachers 59 the next value shall be 00.  The problem I'm having is after the value hits 08 it then goes to 01.

The test code is below to help you. thanks


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script>
  function add_time (obj) {                
      var val=0;
      var ret=0;
    val = (parseInt(obj.value));
      if(val == 59) {
        ret = 0;
      }else{      
          ret = (parseInt(val) + 1);
      }       
      if(parseInt(ret) < 10) {
        ret = ret +"";
        obj.value = "0"+ret;
      }else{
        obj.value = ret;      
      }      
  }
 
  function subtract_time () {
  }  
 
</script>

</head>

<body>
<form>
<table>
  <tr>
    <td><input type="text" value="00" id="minutes" name="minutes"></td>
    <td>
        <input type="button" value="+" onClick="add_time(document.getElementById('minutes'))">
      <input type="button" value="-">              
      </td>      
  </tr>
</table>
</form>
</body>
</html>
LVL 6
dgelinasAsked:
Who is Participating?
 
OzzModCommented:
Closed, no points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0
 
InteractiveMindCommented:
Hmm.. I have no idea why your other code doesn't work (I'm getting the same problem as you), but the following works fine for me:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script language="javascript" type="text/javascript">

  function add_time (obj) {              
     obj.value++;
     if ( obj.value > 59 )
       obj.value = 0;
     
     if ( obj.value < 10 )
       obj.value = "0" + obj.value;  
  }
 
  function subtract_time () {
  }  
 
</script>

</head>

<body>
<form>
<table>
  <tr>
    <td><input type="text" value="00" id="minutes" name="minutes"></td>
    <td>
       <input type="button" value="+" onClick="add_time(document.getElementById('minutes'))">
      <input type="button" value="-">            
     </td>    
  </tr>
</table>
</form>
</body>
</html>

0
 
Infinity08Commented:
The reason that it jumps back to 01 after 08 is because the parseInt() function sees the 08 as an octal number (base 8), and since 08 is an invalid octal number, it returns 0.

If you still want to use the parseInt function, just change all usages of this function from :

parseInt(val)

to

parseInt(val, 10)

specifying that we want the number to be read in base 10, or as a decimal number.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
InteractiveMindCommented:
Ah! Nice one Infinity08  :-)   lol, your username is a little ironic (08(  ^_^
0
 
Infinity08Commented:
:) It is, isn't it ? :) Although the 8 should be viewed on its side ... :)
0
 
ganyewCommented:
//Please try below function

function add_time (obj) {              
     var val=0;
     var ret=0;

     if (obj.value.substring(0,1) == "0")
      ret = parseInt(obj.value.substring(1));
     else
      ret = parseInt(obj.value);

     ++ret;
      if (ret > 59)
       ret = 0; //So that 59 still will display, but if reach 60 the value will change to "00".

      if (ret < 10)
       obj.value = "0" + ret;
      else
        obj.value = ret;    
  }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.