Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am trying to dynamically add up the sum of 5 textboxes into a 6 th text box as the numbers are entered. My script won't run. What am I doing wrong.

XML
<script lang="javascript" type="text/javascript">
            function sum() {
                var TextBoxFirstScoreAnc = document.getElementById('<%= TextBoxFirstScoreAnc.ClientID%>').value;
                var TextBoxAncillaryNonPatScore = document.getElementById('<%= TextBoxAncillaryNonPatScore.ClientID%>').value;
                var TextBoxRestroomScoreAnc = document.getElementById('<%= TextBoxRestroomScoreAnc.ClientID%>').value;
                var TextBoxDustingScoreAnc = document.getElementById('<%= TextBoxDustingCommentsAnc.ClientID%>').value;
                var TextBoxFloorScoreAnc = document.getElementById('<%= TextBoxFloorScoreAnc.ClientID%>').value;

            if ((TextBoxFirstScoreAnc !== "") && (TextBoxAncillaryNonPatScore !== "") && (TextBoxRestroomScoreAnc !== "") && (TextBoxDustingScoreAnc !== "") && (TextBoxFloorScoreAnc !== "")) {
                var newValue = GetDecimalValue(TextBoxFirstScoreAnc) + GetDecimalValue(TextBoxAncillaryNonPatScore) + GetDecimalValue(TextBoxRestroomScoreAnc) + GetDecimalValue(TextBoxDustingScoreAnc) + GetDecimalValue(TextBoxFloorScoreAnc);
                document.getElementById('<%= TextBoxTotalScoreAnc.ClientID%>').value = GetDecimalValue(newValue);
            }
        }
        function GetDecimalValue(value) {
            return (value ? parseFloat(value).toFixed(1) : 0.0);
        }
 </script>
Posted
Comments
Richard Deeming 21-Oct-15 10:30am    
You're missing the code that wires up the sum function to the change events on the textboxes.

Use the "Improve question" button to update your question with the missing code.
F-ES Sitecore 21-Oct-15 10:43am    
There are too many variables to say for sure, it could be anything. Read some articles on how to debug javascript as we can't run your code in situation. At a minimum look at the browser console for errors, and if there are no errors use the "debugger;" keyword to step through your code to see what is happening.

1 solution

Okay, let me carry forward what F-ES Sitecore is saying about the using debugger.
Do the following code, and in browser press F12 and run this function. It'll be paused at the very first line.
Now use the F10 to step into in it and see what is actually happening with your code.
XML
<script lang="javascript" type="text/javascript">
            function sum() {
                debugger;
                var TextBoxFirstScoreAnc = document.getElementById('<%= TextBoxFirstScoreAnc.ClientID%>').value;
                var TextBoxAncillaryNonPatScore = document.getElementById('<%= TextBoxAncillaryNonPatScore.ClientID%>').value;
                var TextBoxRestroomScoreAnc = document.getElementById('<%= TextBoxRestroomScoreAnc.ClientID%>').value;
                var TextBoxDustingScoreAnc = document.getElementById('<%= TextBoxDustingCommentsAnc.ClientID%>').value;
                var TextBoxFloorScoreAnc = document.getElementById('<%= TextBoxFloorScoreAnc.ClientID%>').value;

            if ((TextBoxFirstScoreAnc !== "") && (TextBoxAncillaryNonPatScore !== "") && (TextBoxRestroomScoreAnc !== "") && (TextBoxDustingScoreAnc !== "") && (TextBoxFloorScoreAnc !== "")) {
                var newValue = GetDecimalValue(TextBoxFirstScoreAnc) + GetDecimalValue(TextBoxAncillaryNonPatScore) + GetDecimalValue(TextBoxRestroomScoreAnc) + GetDecimalValue(TextBoxDustingScoreAnc) + GetDecimalValue(TextBoxFloorScoreAnc);
                document.getElementById('<%= TextBoxTotalScoreAnc.ClientID%>').value = GetDecimalValue(newValue);
            }
        }
        function GetDecimalValue(value) {
            return (value ? parseFloat(value).toFixed(1) : 0.0);
        }
 </script>


-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900