Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post

    add time to give total time

    ok picture this
    I have 3 rows each having two input boxes in them
    [] : []
    [] : []
    [] : []
    On the left hand side the boxes are for entering minutes and on the right, entering seconds
    at the bottom there is a total time consisting of three input boxes
    [] .[] : []
    First one on the left for hours, middle for minutes and far right for seconds.

    Mathamatically I take it you need to add all the seconds up then divide that by 60 and and you get the minutes and any remainder is the seconds, then you add the minutes up and divide that by 60 and then those are the hours and any remainders gets added onto the minutes already added via the seconds.

    Now How am I able to make the total time = the time of all three times from above without refreshing the page so it updates automatically.

    Again thank you for any suggestions or ideas

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>48897</title>
    	</head>
    	<body>
    		<form onsubmit="makeTotal();return false;">
    			<table>
    				<thead>
    					<tr>
    						<th>Minutes</th>
    						<th>Seconds</th>
    					</tr>
    				</thead>
    				<tbody>
    					<tr>
    						<td><input type="text"></td>
    						<td><input type="text"></td>
    					</tr>
    					<tr>
    						<td><input type="text"></td>
    						<td><input type="text"></td>
    					</tr>
    					<tr>
    						<td><input type="text"></td>
    						<td><input type="text"></td>
    					</tr>
    				</tbody>
    			</table>
    			<input type="submit">
    		</form>
    
    		<p id="result">
    		</p>
    
    		<script type="text/javascript">
    			var result = document.getElementById("result");
    			result.style.display = "none";
    
    			function makeTotal()
    			{
    				var list = document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input");
    				var total = 0;
    
    				for(var i = 0; i < list.length; i++)
    				{
    					total += (i % 2) ? Number(list[i].value) : Number(list[i].value) * 60;
    				}
    
    				var time = new Date(total * 1000);
    
    				result.firstChild.nodeValue = time.getUTCHours() + ":" + time.getUTCMinutes() + ":" + time.getUTCSeconds();
    				result.style.display = "";
    			}
    		</script>
    	</body>
    </html>

  • #3
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post
    thank you very much although when I click on submit query i get a error box saying "result.firstChild is null or no object"
    and Is it at all posibble to have the total time automatically updating in an inputbox rather than pressing submit all the time

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tsclan
    thank you very much although when I click on submit query i get a error box saying "result.firstChild is null or no object"
    That's possibly because you're using Internet Explorer and I code to web standards. I don't know what IE calls the value of text nodes.
    Quote Originally Posted by tsclan
    Is it at all posibble to have the total time automatically updating in an inputbox rather than pressing submit all the time
    Yes.
    Code:
    <input type="text" onblur="makeTotal()">

  • #5
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post
    Thanks yeah I used it in firefox and it worked fine thanks
    although I am not quite sure where I am to put this line of code
    Code:
    <input type="text" onblur="makeTotal()">
    I am that Incompetent I know :P

    EDIT * WAit No I can figure it out for myself thanks I manged to do it
    must have been feeling lazy back then
    Last edited by tsclan; 12-14-2004 at 12:19 AM.

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •