Y. Vahe Y. Vahe - 3 months ago 8
HTML Question

I cant change value of Input

I want to create

Javascript
simple calculator, I want to change
<input>
tag's value on click, I used this

document.getElementById('input').value = "1"


But it types once 1 so I can't type 11 or 111. What is problem? Can you help me?

Answer

First you have to get the value of that field as

var val = document.getElementById('input').value;

Then Add a value and show in it as

document.getElementById('input').value = val + 1;

Here is a complete Running code:

function function_name (argument) {
		var val = document.getElementById('input').value;		 
	 	document.getElementById('input').value = val + 1;
	}
<input id="input" type="text" name="name" value="">
    <button id="button" onclick="function_name()">Press 1!</button>