user2838042
user2838042

Reputation: 57

how to return the output of a JS function to an HTML button 'id'

I have an html page that needs to store the time at the point of button click back on to the database. I have a JS function that calculate the time.But somehow the html button is not holding the value.Here is the code.

    <html>
    <head>
    <script>          
       
function checkTime(i)
{
    if (i<10)
{
       i="0" + i;
    }
    return i;
      }
function checkMsec(j)
 {
        if ( (j<100) && (j<10))
        {
        j="00" + j;
     }
        if ((j<100) && (j>10))
        {
     j="0" + j;
         }
        return j;
      }   
    function timeCheck(stTime)
      {
     var currentTime=new Date();
     var cDat = currentTime.getDate();
         var cMon = currentTime.getMonth() + 1; 
         var cYear = currentTime.getFullYear();
         var h = currentTime.getHours();
         var m = currentTime.getMinutes();
         var s = currentTime.getSeconds();
     var ms = currentTime.getMilliseconds();
    
      m = checkTime(m);
      s = checkTime(s);
      ms = checkTime(ms);
      stTime = cDat+"-"+cMon+"-"+cYear+" "+h+":"+m+":"+s+":"+ms;
      //alert(stTime); // this is displaying the time as an alert
      return stTime; //Here I am expecting the id attribute of the button will have the stTime
      
      }
    </script>
    </head>
    <body>
 <form name ="frm1" method ="POST" action="<% gait_url %>">

     <p>Time 1: <input type="button" name=start1 value= "Start" id="start1" onClick ="timeCheck(this)">

     <input type="button" name=stop1 id="stop1" value= "Stop" onClick ="timeCheck(this)"></p>

     <p>Time 2: <input type="button" name=start2 value= "Start" id="start2" onClick ="timeCheck(this)">

     <input type="button" name=stop2 id="stop2" value= "Stop" onClick ="timeCheck(this)">   </p>

<input type="submit" value="Back to Menu" name="Back to Menu">

</form>

    </body>
    </html>

Upvotes: 0

Views: 113

Answers (1)

Asons
Asons

Reputation: 87191

The return value goes to the object's event method, not to the objects property "id".

Change this

stTime = cDat+"-"+cMon+"-"+cYear+" "+h+":"+m+":"+s+":"+ms;
//alert(stTime); // this is displaying the time as an alert
return stTime; //Here I am expecting the id attribute of the button will have the stTime

to

stTime.id = cDat+"-"+cMon+"-"+cYear+" "+h+":"+m+":"+s+":"+ms;

Upvotes: 1

Related Questions