Skip to content Skip to sidebar Skip to footer

Add Two Numbers And Display Result In Textbox With Javascript

I was just trying to write a simple javascript program that will demonstrate to take user input from text field, and clicking the button will display the summation result of those

Solution 1:

Here a working fiddle: http://jsfiddle.net/sjh36otu/

functionadd_number() {

            var first_number = parseInt(document.getElementById("Text1").value);
            var second_number = parseInt(document.getElementById("Text2").value);
            var result = first_number + second_number;

            document.getElementById("txtresult").value = result;
        }

Solution 2:

When you assign your variables "first_number" and "second_number", you need to change "document.getElementsById" to the singular "document.getElementById".

Solution 3:

var first_number = parseInt(document.getElementById("Text1").value);
var second_number = parseInt(document.getElementById("Text2").value);

// This is because your method .getElementById has the letter 's': .getElement**s**ById

Solution 4:

<script>functionsum()
{
    var value1= parseInt(document.getElementById("txtfirst").value);
    var value2=parseInt(document.getElementById("txtsecond").value);
    var sum=value1+value2;
    document.getElementById("result").value=sum;

}
 </script>

Solution 5:

You made a simple mistake. Don't worry.... Simply use getElementById instead getElementsById

true

var first_number = parseInt(document.getElementById("Text1").value);

False

var first_number = parseInt(document.getElementsById("Text1").value);

Thanks ...

Post a Comment for "Add Two Numbers And Display Result In Textbox With Javascript"