0

I am a beginner to Javascript and am trying to perform arithmetic functions.

Here is my code below:

script

<script language="javascript" type="text/javascript">
   function func(a,b) 
     {
        var a = document.getElementById("a");
        var b = document.getElementById("b");
        if (document.getElementById("btnadd").Text == 'Add') 
          {
              var c = a + b;
              document.getElementById("rslt").innerHTML =c;           
          }    
     }
</script>

aspx

<asp:Button ID="btnadd" Text="Add" runat="server" OnClientClick="func(txtn1,txtn2)" />
<p id="rslt"></p>

My idea is, when I click say 'add' button the value of two textbox should be passed to the script and are assigned to two variables.

With those two variables all arithmetic (add,sub,div,mul) should be done.

0

4 Answers 4

0

What you could do is also pass the button that was clicked, using this. That way you know the text of the button that was clicked. And since you are using a server side button, be sure to include return false; from your JavaScript call to prevent it from posting back and clearing your <p> tag.

<asp:Button ID="btnadd" runat="server" Text="Add"
    OnClientClick="func('txtn1', 'txtn2', this); return false;" />
function func(a, b, btn) {
    var txtn1 = document.getElementById(a);
    var txtn2 = document.getElementById(b);
    var rslt = document.getElementById('rslt');
    if(btn.value == 'Add')
        rslt.innerHTML = parseInt(txtn1.value) + parseInt(txtn2.value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

According to here:

You have to use anonymous functions/handlers to react on such user-interactions without reloading the page. Also look at jquery (http://api.jquery.com/click/) which gives you a simpler api to write those things.

<script language="javascript" type="text/javascript">
    function func(a,b) {
        var a = document.getElementById("a");
        var b = document.getElementById("b");
        var btn = document.getElementsById("btnadd"); 
        btn.onclick = function() { 
          var c = a + b;
          // alert("Result = " + c ); 
          document.getElementById("rslt").innerHTML =c;     
        }
    }
</script>

1 Comment

i cant view the output in the window. could please help in that. i want to print output in the <p> tag in aspx.
0

Hope that your requirement is to perform arithmetic operations with two numbers from the text boxes.For that you need not pass text boxes as parameters to the script since it uses document.getElementById. directly access the textbox inside the script as follows:

<head><script>
      function myFunction(a) {
        var y = document.getElementById("txt1").value;
        var z = document.getElementById("txt2").value;
        var x;
        if(a==1)
         x = +y + +z;
        else if(a==2)
         x=+y - +z;
         else
         x=0;
        document.getElementById("demo").innerHTML = x;
      }
    </script></head>
<body>
    <p>Click the button to calculate x.</p>       
    <br/>
    <br/>Enter first number:
    <input type="text" id="txt1" name="text1">Enter second number:
    <input type="text" id="txt2" name="text2">
     <button onclick="myFunction(1)">Addition</button>
      <button onclick="myFunction(2)">Subtraction</button>
    <p id="demo"></p>        
  </body>

The above code will perform addition and subtraction based on button click

Comments

0
function func(a,b) {
        var a,b; 
        a=$("#a").val();
        b=$("#b").val();
        if ($("#btnAdd").val() == 'Add') {    
             $("#a").val(+(a) + +(b));
        }

    }

And Set button value to Add

Or if you want to reduce your code then use this code

       if ($("#btnAdd").val() == 'Add') {    
             $("#a").val( +($("#a").val()) + +($("#b").val() ));
        }

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.