I am trying to populate a text box on a form by clicking on form buttons. Below is code I have so far – modified this code from a select box example –
<!DOCTYPE html>
<html>
<head>
<script>
function moveNumbers(){
var no=document.getElementById("no");
var txt=document.getElementById("result").value;
txt=txt + option;
document.getElementById("result").value=txt;
}
</script>
</head>
<body>
<form>
Select numbers:<br>
<input type="button" value="1" name="no" onclick="moveNumbers()">
<input type="button" value="2" name="no" onclick="moveNumbers()">
<input type="button" value="3" name="no" onclick="moveNumbers()">
<input type="text" id="result" size="20">
</form>
</body>
</html>
There are a few flaws here. It doesn’t seem like
optionis defined. And you have no way to retrieve the button that was actually clicked.What you can do is pass
this.valueto your onclick event handler. This passes the value of the button you push, and use that to append to your textbox value.http://jsfiddle.net/cMN44/