Odd behaviour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #1

    Odd behaviour

    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?

    Code:
     okToAdd = false
     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!");
     if (cReset == true)
     {
     document.getElementById(cEnd).select();
     document.getElementById(cEnd).focus();
     return false;
     }else{
     document.getElementById(cStart).value = "";
     document.getElementById(cEnd).value = "";
     document.getElementById(cStart).focus();
     return false;
     }
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    One problem is that this line needs to be terminated with a semi-colon.

    Code:
    okToAdd = false
    Should be :
    Code:
    okToAdd = false;

    Comment

    • Logician
      New Member
      • Feb 2007
      • 210

      #3
      Originally posted by Claus Mygind
      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.

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Originally posted by pronerd
        One problem is that this line needs to be terminated with a semi-colon.

        Code:
        okToAdd = false
        Should be :
        Code:
        okToAdd = false;

        I have added the ; to the line suggested and the problem persists.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by pronerd
          One problem is that this line needs to be terminated with a semi-colon.

          Code:
          okToAdd = false
          Should be :
          Code:
          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.. ;)

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Code:
            ar cReset = confirm("Test Confirm Box!!!");
            if (cReset == true)
            {
            alert('OK');
            return false;
            }else{
            alert('Cancel');
            return false;
            }
            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 ?

            Comment

            • Claus Mygind
              Contributor
              • Mar 2008
              • 571

              #7
              Originally posted by Logician
              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

              Code:
              	//make new cell
              	td = tr.insertCell(tr.cells.length);
              
              	td.innerHTML =	'<input '+
              					'type="text" '+
              					'id  ="STARTMILES~'+cNextLine+'" '+
              					'name="STARTMILES~'+cNextLine+'" '+
              					'class="InputText" '+
              					'size ="9" '+
              					'maxlength="8" '+
              					'onKeyPress="return isDollar(event);" '+
              					'onchange ="if (cSetLoc(this)) {DataChanged();} updateMiles(this);" '+
              					'/>';
              
              	//make new cell
              	td = tr.insertCell(tr.cells.length);
              
              	td.innerHTML =	'<input '+
              					'type="text" '+
              					'id  ="ENDMILES~'+cNextLine+'" '+
              					'name="ENDMILES~'+cNextLine+'" '+
              					'class="InputText" '+
              					'size ="9" '+
              					'maxlength="8" '+
              					'onKeyPress="return isDollar(event);" '+
              					'onchange ="if (cSetLoc(this)) {DataChanged();} updateMiles(this);" '+
              					'/>';
              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.

              Code:
              function cSetLoc(elem)
              {
              	aLoc = elem.name.split('~');
              	cLoc = aLoc[1];
              	return true;
              }
              then it marks that a field on the form has been changed in the DataChanged() function.

              then the updateMiles(thi s) function is called
              Code:
              function updateMiles(obj)
              {
              	var cEnd = "ENDMILES~"+cLoc;
              	var cStart = "STARTMILES~"+cLoc;
              	var okToAdd = true;
              
              	if (obj.id.substring(0,1) == "E")
              	{
              		if (document.getElementById(cStart).value == "")
              		{
              			okToAdd = false;
              			alert("Your Start reading is blank! \n Enter Start Miles first!");
              			document.getElementById(cEnd).value = "";
              			document.getElementById(cStart).focus();
              			return false;
              		}else{
              			if ( parseFloat(obj.value ) < parseFloat( document.getElementById(cStart).value) )
              			{
              				okToAdd = false;
              				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!");
              				if (cReset == true)
              				{
              					document.getElementById(cEnd).select();
              					document.getElementById(cEnd).focus();
              					return false;
              				}else{
              					document.getElementById(cStart).value = "";
              					document.getElementById(cEnd).value = "";
              					document.getElementById(cStart).focus();
              					return false;
              				}
              			}else{
              				aDailyTime[cLoc-1].ENDMILES = obj.value;
              			}
              		}
              	}else{
              		okToAdd = false;
              		aDailyTime[cLoc-1].STARTMILES = obj.value;
              		//if end miles is not blank then retotal milage
              		if (cEnd > "" )
              		{
              			okToAdd = true;
              		}
              	}
              
              	if (okToAdd)
              	{
              		document.getElementById("MILES~"+cLoc).value = (parseFloat(document.getElementById(cEnd).value) - parseFloat(document.getElementById(cStart).value)).toFixed(1);
              		aDailyTime[cLoc-1].MILES = parseFloat(document.getElementById("MILES~"+cLoc).value);
              		var result = 0;
              		for (var i = 0; i < aDailyTime.length; i++ )
              		{
              			result += parseFloat(aDailyTime[i].MILES);
              		}
              		document.getElementById("totMiles").value =result.toFixed(1);
              	}
              }
              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.

              Code:
              'onchange ="if (cSetLoc(this)) {DataChanged();} return updateMiles(this);" '+

              Comment

              • Claus Mygind
                Contributor
                • Mar 2008
                • 571

                #8
                Originally posted by dmjpro
                Code:
                ar cReset = confirm("Test Confirm Box!!!");
                if (cReset == true)
                {
                alert('OK');
                return false;
                }else{
                alert('Cancel');
                return false;
                }
                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.

                Comment

                Working...