1

this is my javascript to get addition of 2 values from 2 textboxes into the third....

its not working...

<script type="text/javascript">

    function calculate(ctrl1, ctrl2, ctrl3) {

        var c1 = document.getElementById(ctrl1);
        var c2 = document.getElementById(ctrl2);
        var c3 = document.getElementById(ctrl3);

        if (c1 != null && c2 != null & c3 != null) {
            c3.value = Number(c1.value) + Number(c2.value);

        }
        document.forms[0].txteanum.focus();

    }    
</script>

in textboxes

<asp:TextBox ID="txtQuantity" runat="server" onblur='javascript:calculate("txtQuantity","txtRate","TxtAmount")'></asp:TextBox>

check my answer here https://stackoverflow.com/a/11624756/1445836

5
  • 2
    Is the capital "T" in "TxtAmount" a typo? (You have lower-case "t" in the other fields.) If you View Source in the browser are those ids the ones actually produced from the ASP code? Commented Jul 23, 2012 at 12:21
  • could you write an else part and put an alert / console.log to find if any of those textboxes are null? Commented Jul 23, 2012 at 12:26
  • Your code works for me: jsfiddle.net/r3SvQ so I assume what I mentioned in the previous comment is the problem. Try adding a ClientIDMode="Static" attribute to your inputs... Commented Jul 23, 2012 at 12:38
  • See @hasanchuck's answer - you are using server side controls and their IDs may not be what you expect. Commented Jul 23, 2012 at 13:45
  • got my answer stackoverflow.com/a/11624756/1445836 Commented Jul 24, 2012 at 5:39

3 Answers 3

3

Correct: if (c1 != null && c2 != null & c3 != null)

TO: if (c1 != null && c2 != null && c3 != null) Missing &

To commenters: Sorry I never heard of Number(), since I usually don't work with front-end (especially math in front-end), mostly PHP/C#.

Also, c2, c3 are <input>'s ?

Sign up to request clarification or add additional context in comments.

3 Comments

Good catch on the &, except that it doesn't actually matter: jsfiddle.net/r3SvQ
@GuyDavid: +1 on the & catch. Also, it would be better if OP uses ClientIDMode="Static". It might be a case of ID mangling.
Effectively the operands are converted to numbers such that in practice true & true returns 1 and true & false or other combinations with false return 0. See Bitwise logical operators at MDN.
2

Maybe You can't get textboxes. Have you tried to give them clientidmode static?

<asp:TextBox ID="txtQuantity" runat="server" onblur='javascript:calculate("txtQuantity","txtRate","TxtAmount")' ClientIDMode="static"></asp:TextBox>

Comments

0

here is the java script which worked for me

function Sum() {
                        var text1 = document.getElementById('<%= TextBox1.ClientID %>');
                        var text2 = document.getElementById('<%= TextBox2.ClientID %>');
                        if (text1.value.length == 0 || text2.value.length == 0) {
                                alert('Textbox1 and Textbox2 should not be empty');
                                return;
                        }

                        var x = parseInt(text1.value);
                        var y = parseInt(text2.value);
                        document.getElementById('<%= TextBox3.ClientID %>').value = x +y;
                }

into the textboxes in .aspx page itself

<asp:TextBox ID="TextBox1" runat="server" onblur="Sum()"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" onblur="Sum()"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.