javascript add textbox value by click on button

JavaScript On Button Click add value to Textbox Dynamicaly


Introduction : Here In this tutorial explain how to add multiple button value dynamically to the text box and we can sum all the button value and display it on text box as below using javascript.


Step 1:  First add JavaScript code to head section of html Source as Below.


<script>

    function FunNumber(num) {
        var txt = parseInt(document.getElementById("txt").value);

        if (isNaN(txt)) {
            txt = 0;
            var NUM = parseInt(num);

            document.getElementById("txt").value = NUM + txt;
        }
        else {
            var NUM = parseInt(num);
            document.getElementById("txt").value = NUM + txt;
        }
    }

</script>


Step 2:   Now add TextBox and Button as below


<body>

<input type="text" id="txt" value size="40">


<input type="button" value="5"  onclick="FunNumber(this.value)">  
<input type="button" value="10"  onclick="FunNumber(this.value)"> 
<input type="button" value="20"  onclick="FunNumber(this.value)">  
<input type="button" value="40"  onclick="FunNumber(this.value)">   



</body>


Previous
Next Post »