Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

document.getelementbyid add number

function add(){
"use strict";
        var num1 = parseInt(document.getElementById("firstNumber").value);
        //You need to define your variable before use.
        var num2 = parseInt(document.getElementById("secondNumber").value);
        console.log(document.getElementById("firstNumber"));
        //this is a DOM object
        console.log(typeof document.getElementById("firstNumber").value);
        //this is a "string"
        console.log(typeof parseInt(document.getElementById("firstNumber").value));
        console.log(typeof +document.getElementById("firstNumber").value);
        //quickly  by use '+'
        document.getElementById("result").innerHTML =num1+num2;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #add #number
ADD COMMENT
Topic
Name
5+3 =