www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Using the Enter key to move to the next field

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    resolved [RESOLVED] Using the Enter key to move to the next field

    I have a simple timesheet calculator and, since it would be used with the numeric keypad on the right, how could I produce intermediate results and go to the next field when the Enter or + key was entered?

    This is my script from the above page.
    Code:
    function calculate() {
      var pattn = /\D*(\d+)\D+(\d+)\D*/;
      var localBeg, localEnd, totalMin=0, result = new Array;
      for (var i=0; i < 60; i+=3) {
        result = document.timeform.elements[i].value.match(pattn);
    	if (result == null)
    	  continue;
    	else {
          localBeg = parseInt(result[1]) * 60 + parseInt(result[2]);
          result = document.timeform.elements[i+1].value.match(pattn);
          if (result == null) {
    	    document.timeform.elements[i+1].value = "missing";
    		continue;}	
          localEnd = parseInt(result[1]) * 60 + parseInt(result[2]);
          if (localEnd < localBeg)
            localEnd += 720; // Add 12 hours worth of minutes, e.g., back from lunch at 12:45, leave at 5:20
    	  var thisCell = localEnd - localBeg;
    	  var localMin = thisCell % 60;
    	  document.timeform.elements[i+2].value = Math.floor(thisCell/60) + ":" + (localMin < 10 ? "0"+localMin : localMin);
          totalMin += localEnd - localBeg;
    	}  }
    	var dispHours = Math.round(totalMin / 60 * 1000) / 1000;
    	document.timeform.totals.value = dispHours;    
    }
    and every third element is disabled to show the elapsed time in the first two.
    Code:
    <form name="timeform">
    <table>
    <tr><th>Beg Time</th><th>End Time</th><th></th><th>Beg Time</th><th>End Time</th><th></th></tr>
    <tr><td><input type="text" size=8></td><td><input type="text" size=8></td><td><input type="text" size=8 disabled>
    </td><td><input type="text" size=8></td><td><input type="text" size=8></td><td><input type="text" size=8 disabled></td></tr>
    Thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,213
    You could fill in the blanks with a full form that you are using, jut one row of entries isn't much to go on like how is the form to calculate?

    Whats wrong with using the Tab key and setting tab indexes?

    You could have the tab indexes set, capture the return key event and read the current tab index and then set focus to the next field tab index.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Quote Originally Posted by \\.\ View Post
    You could have the tab indexes set, capture the return key event and read the current tab index and then set focus to the next field tab index.
    I added the following code
    Code:
    var j;
    document.onkeypress = keyHit;
    function keyHit(evt) {
    var e = evt || window.event;
    if (document.timeform.DeBugging.checked==false) {
      j=0;
      return true; }
    if (e.keyCode == 13) {
      if (j%3) {
        calculate();
        ++j; }
      document.timeform.elements[++j].focus();  
    }
    return true;
    }
    to the link in my first post and it seems to run as advertised. Thanks for the tip.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,213
    No Problem, it gave me a few ideas on messing around with creating id's on the fly and auto generating tab ID's and resolving row and column references for a set of tab indexes set out in the configuration of a 6 x 7 grid.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Posting Permissions

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



Recent Articles