简体   繁体   中英

How to send value of button to textBox?

I want to create a button with value = "7" (string) or value = 7 (double).

Also i want a textbox, that when i click said button the number 7 or the string "7" should also get written in that textbox.

My button:

<input type="button" name="btnSeven" id="btnSeven" value="7" onclick="setText7()"  />

My textBox:

<input type="text" name="textBox" id="textBox" value=""/>

here you go:

window.onload = function() {
    document.getElementById('btnSeven').addEventListener('click', function(){
        document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnSeven').value;
    });
}

when getting a .value from a button object, it will always return a string, event if it's an int or double.

hope that helped.

Use following :

<script>
    function setText7(obj){
    var val = obj.value;
    console.log(val);
    document.getElementById('textBox').value = val;
}
</script>
<input type="button" name="btnSeven" id="btnSeven" value="7" onclick="setText7(this)">
<input type="text" name="textBox" id="textBox" value=""/>

Here is the working demo : http://jsfiddle.net/sh3Mz/

<div class="container">
    <input type="button" name="btnSeven" id="btnSeven" value="7" onclick="getthevalue()" />
    <input type="text" name="textBox" id="textBox" value="" />
    <h3 id="res"></h3>
</div>
function getthevalue() {
    document.getElementById('btnSeven').addEventListener('click', function() {
        document.getElementById('textBox').value = document.getElementById('textBox').value +
            document.getElementById('btnSeven').value;
    });
};
function getthevalue() { document.getElementById('btnSeven').addEventListener('click', function(){ document.getElementById('textBox').value = document.getElementById('textBox').value+ document.getElementById(' btnSeven').value; })};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM