Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
This is my javascript code:-

XML
<script language="javascript" type="text/javascript">

            function functn() {
                var a = document.getElementById('TextBox1').value;
                var b = document.getElementById('TextBox2').value;
                if (a == "")
                    a = 0;
               else if (b == "")
                    b = 0;

                var res = parseInt(a) + parseInt(b);
                if (!NaN(res)) {
                    document.getElementById('TextBox3').value = res;
                }

                else
                    document.getElementById('TextBox3').value = 0;
            }

        </script>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>


Can't figure out where I'm wrong. Need HELP!!
Posted
Comments
Andy Lanng 14-Apr-15 11:41am    
How are you expecting the function to be run?
If it runs then have you debugged the function?
If you have then where is the error?
PS: jQuery will probably come up in many answers. Is that something you've looked into before?
ZurdoDev 14-Apr-15 11:54am    
Just debug it and you'll see what the issue is.
Sergey Alexandrovich Kryukov 14-Apr-15 13:04pm    
There are apparent problems I can see. Please see my answer.
—SA
ZurdoDev 14-Apr-15 13:28pm    
a is being pulled from a textbox so if no value is put in, it will be empty string "". That is perfectly valid.
Sergey Alexandrovich Kryukov 14-Apr-15 15:22pm    
Ah, right; I failed to see it. Will remove the answer.
Thank you very much for telling me about my mistake.
—SA

There is several bad thing in your coding.

JavaScript
function functn() {
    //no problem
    var a = document.getElementById('TextBox1').value;
    //no problem
    var b = document.getElementById('TextBox2').value;
    if (a == "")
        a = 0;
//problem
//why:
//if a=="" then next statement do not get verified
//what to do? : remove else
   else if (b == "")
        b = 0;
//no problem
    var res = parseInt(a) + parseInt(b);
    //problem;
    //big problem; 
    //if you check on debugger ">typeof NaN", you will get "number"; NaN is not a function. you are suppose to check isNaN; your problem will be solved, I guess.
    if (!NaN(res)) {
        document.getElementById('TextBox3').value = res;
    }
    else
        document.getElementById('TextBox3').value = 0;
//other than the one compilation error and one logical error everything seems fine. 
}



How to debug: I use three software to debug web related issues
1. Firefox : (ctrl+shift+k)
2. Chrome : (ctrl+shift+j)
3. Internet Explorer(11); occasionally(F12)

so, open, say Chrome, press ctrl+shift+j
and then write, in the newly opened console, functn()
press enter
you will see your function error;

anyway, give it a shot.
 
Share this answer
 
Comments
Deepak Kanswal Sharma 15-Apr-15 2:29am    
Thanks buddy,

But I'm a little bit confused though.
How can I verify, weather the user have given anything in textbox or not.

I want if there is no value in the textbox then the total gives 0.
Is this the code then:-

if (res==0) {
document.getElementById('TextBox3').value = res;
}
else
document.getElementById('TextBox3').value = 0;
Mohibur Rashid 15-Apr-15 5:50am    
For empty string checking: just remove the else keyword. Conditions are fine.
Deepak Kanswal Sharma 15-Apr-15 12:56pm    
So I tried the debug. This is what I got:-

Uncaught SyntaxError: Unexpected end of inputmessage: "Unexpected end of input"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: ErrorVM93:847 InjectedScript._evaluateOnVM93:780 InjectedScript._evaluateAndWrapVM93:646 InjectedScript.evaluate
So I found a solution with the button click event:-

XML
<script language="javascript" type="text/javascript">

            function functn() {
                var a = document.getElementById('TextBox1').value;
                var b = document.getElementById('TextBox2').value;
                if (a == "")
                    a = 0;
                if (b == "")
                    b = 0;

                var res = parseInt(a) + parseInt(b);
                //if (!NaN(res))
                {
                    document.getElementById('TextBox3').value = res;
                }


            }

        </script>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <button id="Button7" value="Click" onclick="functn()">Click</button>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>



But I want no buttons.

What I want is, as soon as I leave the 2nd textbox, the 3rd textbox gets the sum automatically.
 
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