By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

Odd behaviour

Claus Mygind
100+
P: 571
My code is not branching as expected. In the code below after the confirm message if the user click "OK" the branching does take the true route. But then it continues to the false route. Why would that occur?

Expand|Select|Wrap|Line Numbers
  1.  okToAdd = false
  2.  var cReset = confirm("Your ending odometer reading is less than your start. \n Press OK to change or \n Cancel to set both start and end miles to blank!");
  3.  if (cReset == true)
  4.  {
  5.  document.getElementById(cEnd).select();
  6.  document.getElementById(cEnd).focus();
  7.  return false;
  8.  }else{
  9.  document.getElementById(cStart).value = "";
  10.  document.getElementById(cEnd).value = "";
  11.  document.getElementById(cStart).focus();
  12.  return false;
  13.  }
  14.  
Sep 23 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 392
One problem is that this line needs to be terminated with a semi-colon.

Expand|Select|Wrap|Line Numbers
  1. okToAdd = false
Should be :
Expand|Select|Wrap|Line Numbers
  1. okToAdd = false;
Sep 23 '08 #2

100+
P: 210
My code is not branching as expected. In the code below after the confirm message if the user click "OK" the branching does take the true route. But then it continues to the false route. Why would that occur?
With a return statement I don't see how that can happen. If the focus is immediately lost, the cause is probably the button on the prompt still being 'pressed'.
If that's not it then you need to show more.
Sep 23 '08 #3

Claus Mygind
100+
P: 571
One problem is that this line needs to be terminated with a semi-colon.

Expand|Select|Wrap|Line Numbers
  1. okToAdd = false
Should be :
Expand|Select|Wrap|Line Numbers
  1. okToAdd = false;

I have added the ; to the line suggested and the problem persists.
Sep 29 '08 #4

dmjpro
100+
P: 2,476
One problem is that this line needs to be terminated with a semi-colon.

Expand|Select|Wrap|Line Numbers
  1. okToAdd = false
Should be :
Expand|Select|Wrap|Line Numbers
  1. okToAdd = false;
I think in JavaScript ; matters when more than one statements are in a single line, otherwise it is meaningless ..it's nothing but a good programming practice. As for myself i do Java and JavaScript both so i need to have practice to put semicolon after each statement.. ;)
Sep 29 '08 #5

dmjpro
100+
P: 2,476
Expand|Select|Wrap|Line Numbers
  1. ar cReset = confirm("Test Confirm Box!!!");
  2. if (cReset == true)
  3. {
  4. alert('OK');
  5. return false;
  6. }else{
  7. alert('Cancel');
  8. return false;
  9. }
  10.  
I call this code on loading the form ..... it works as it supposed to do ... ;)
One thing do ..... try to check the value what confirm returns ?
Sep 29 '08 #6

Claus Mygind
100+
P: 571
With a return statement I don't see how that can happen. If the focus is immediately lost, the cause is probably the button on the prompt still being 'pressed'.
If that's not it then you need to show more.
The confirm dialogue box disappears after the user click's

Here is the code.

First the two input fields that call this function

Expand|Select|Wrap|Line Numbers
  1.     //make new cell
  2.     td = tr.insertCell(tr.cells.length);
  3.  
  4.     td.innerHTML =    '<input '+
  5.                     'type="text" '+
  6.                     'id  ="STARTMILES~'+cNextLine+'" '+
  7.                     'name="STARTMILES~'+cNextLine+'" '+
  8.                     'class="InputText" '+
  9.                     'size ="9" '+
  10.                     'maxlength="8" '+
  11.                     'onKeyPress="return isDollar(event);" '+
  12.                     'onchange ="if (cSetLoc(this)) {DataChanged();} updateMiles(this);" '+
  13.                     '/>';
  14.  
  15.     //make new cell
  16.     td = tr.insertCell(tr.cells.length);
  17.  
  18.     td.innerHTML =    '<input '+
  19.                     'type="text" '+
  20.                     'id  ="ENDMILES~'+cNextLine+'" '+
  21.                     'name="ENDMILES~'+cNextLine+'" '+
  22.                     'class="InputText" '+
  23.                     'size ="9" '+
  24.                     'maxlength="8" '+
  25.                     'onKeyPress="return isDollar(event);" '+
  26.                     'onchange ="if (cSetLoc(this)) {DataChanged();} updateMiles(this);" '+
  27.                     '/>';
  28.  
In this code:

cNextLine - is a variable which increments with each new line created per the user's needs.

isDollar(event) - is simply a function that checks that only numbers or a decimal point was entered.

onchange - event handler first marks the line on which the user is working by updating the variable cLoc. The default is always true because I just want to capture the position of the user's cursor.

Expand|Select|Wrap|Line Numbers
  1. function cSetLoc(elem)
  2. {
  3.     aLoc = elem.name.split('~');
  4.     cLoc = aLoc[1];
  5.     return true;
  6. }
  7.  
then it marks that a field on the form has been changed in the DataChanged() function.

then the updateMiles(this) function is called
Expand|Select|Wrap|Line Numbers
  1. function updateMiles(obj)
  2. {
  3.     var cEnd = "ENDMILES~"+cLoc;
  4.     var cStart = "STARTMILES~"+cLoc;
  5.     var okToAdd = true;
  6.  
  7.     if (obj.id.substring(0,1) == "E")
  8.     {
  9.         if (document.getElementById(cStart).value == "")
  10.         {
  11.             okToAdd = false;
  12.             alert("Your Start reading is blank! \n Enter Start Miles first!");
  13.             document.getElementById(cEnd).value = "";
  14.             document.getElementById(cStart).focus();
  15.             return false;
  16.         }else{
  17.             if ( parseFloat(obj.value ) < parseFloat( document.getElementById(cStart).value) )
  18.             {
  19.                 okToAdd = false;
  20.                 var cReset = confirm("Your ending odometer reading is less than your start. \n Press OK to change or \n Cancel to set both start and end miles to blank!");
  21.                 if (cReset == true)
  22.                 {
  23.                     document.getElementById(cEnd).select();
  24.                     document.getElementById(cEnd).focus();
  25.                     return false;
  26.                 }else{
  27.                     document.getElementById(cStart).value = "";
  28.                     document.getElementById(cEnd).value = "";
  29.                     document.getElementById(cStart).focus();
  30.                     return false;
  31.                 }
  32.             }else{
  33.                 aDailyTime[cLoc-1].ENDMILES = obj.value;
  34.             }
  35.         }
  36.     }else{
  37.         okToAdd = false;
  38.         aDailyTime[cLoc-1].STARTMILES = obj.value;
  39.         //if end miles is not blank then retotal milage
  40.         if (cEnd > "" )
  41.         {
  42.             okToAdd = true;
  43.         }
  44.     }
  45.  
  46.     if (okToAdd)
  47.     {
  48.         document.getElementById("MILES~"+cLoc).value = (parseFloat(document.getElementById(cEnd).value) - parseFloat(document.getElementById(cStart).value)).toFixed(1);
  49.         aDailyTime[cLoc-1].MILES = parseFloat(document.getElementById("MILES~"+cLoc).value);
  50.         var result = 0;
  51.         for (var i = 0; i < aDailyTime.length; i++ )
  52.         {
  53.             result += parseFloat(aDailyTime[i].MILES);
  54.         }
  55.         document.getElementById("totMiles").value =result.toFixed(1);
  56.     }
  57. }
  58.  
Following the flow of the coding with firebug in firefox, I see it follows the correct flow and returns to the "onchange()" event handler shown in the first coding box of this post. From there it simply advances to the next field and focus on the endMiles field is lost.

I suppose this could be happening since the updateMiles function is not a condition. So I have tried this code which also did not work.

Expand|Select|Wrap|Line Numbers
  1. 'onchange ="if (cSetLoc(this)) {DataChanged();} return updateMiles(this);" '+
  2.  
Sep 29 '08 #7

Claus Mygind
100+
P: 571
Expand|Select|Wrap|Line Numbers
  1. ar cReset = confirm("Test Confirm Box!!!");
  2. if (cReset == true)
  3. {
  4. alert('OK');
  5. return false;
  6. }else{
  7. alert('Cancel');
  8. return false;
  9. }
  10.  
I call this code on loading the form ..... it works as it supposed to do ... ;)
One thing do ..... try to check the value what confirm returns ?

Thank you for the reply, I no longer have a problem with the proper branching, but with not setting focus to the invalid field. Instead the cursor simply moves to the next field.
Sep 29 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.