Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two textbox values and display the sum in a third textbox automatically

Tags:

javascript

I have assigned a task to add two textbox values.I want the result of addition to appear in the 3rd textbox,as soon as enter the values in the first two textboxes,without pressing any buttons.

For eg:In the first textbox i want to enter 450,when i press digit 4 of number '450',then it will be added to the 3rd textbox,any number i press in the first two textboxes,suddenly that changes will be reflected on the third textbox.How can i do this?

Here i write my code call sum() in onkeyup

onkeyup="sum()"
function sum() {
        var txtFirstNumberValue = document.getElementById('txt1').value;
        var txtSecondNumberValue = document.getElementById('txt2').value;
        var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        if(!isNaN(result)){
            document.getElementById('txt3').value = result;
        }
}

This is not working in chrome

like image 820
Monica Avatar asked May 21 '13 11:05

Monica


People also ask

How to add multiple TextBox values in JavaScript?

addEventListener("keyup",()=>{ //Get value of all boxes using . value as na,bb,nc,..... var sum = na+nb+nc+.....; Console. log(sum); }); If it's not clear to you first complete dom of js.

How can I add two TextBox values in Visual Basic?

Text = Convert. toInt32(textbox1. Text) + Convert. toInt32(textbox2.


1 Answers

try this

  function sum() {
       var txtFirstNumberValue = document.getElementById('txt1').value;
       var txtSecondNumberValue = document.getElementById('txt2').value;
       if (txtFirstNumberValue == "")
           txtFirstNumberValue = 0;
       if (txtSecondNumberValue == "")
           txtSecondNumberValue = 0;

       var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
       if (!isNaN(result)) {
           document.getElementById('txt3').value = result;
       }
   }
like image 173
Qasim Javaid Khan Avatar answered Sep 28 '22 03:09

Qasim Javaid Khan