简体   繁体   中英

How to copy text from one textbox to another after you push a button in JavaScript/HTML

So here is my code:

<input type="text" id="input">
<button>Go</button><br>
<input type="text" id="output">

So what I want to do is when you type something into the first box and press go it prints it to the second box. If anyone could help me then thank you.

Try like this

HTML

<input type="text" id="input">
<button onclick="copyText()">Go</button><br>
<input type="text" id="output">

JS

function copyText(){
   document.getElementById("output").value=document.getElementById("input").value;
}
<input type="text" id="input">
<button onclick='change()'>Go</button><br>
<input type="text" id="output">
    <script>
        function change(){
            var inputValue=document.getElementById('input').value;
            var output = document.getElementById('output');
            output.value=inputValue;
        }


    </script>

You need to add the onclick event and define a function, in this case I define the change() function. Inside the function you get the text in the textfield with the id input and then you have to set that value to the other textfield (output) with output.value=inputvalue

You can use jQuery: http://jsfiddle.net/e49wqear/ .

$(function() {
    $("button").click(function(e) {
        $("#output").val($("#input").val());    
    });
});

If you add an id to the button, you could do this:

document.getElementById('go').onclick = function() {
  document.getElementById("output").value = document.getElementById("input").value;
}

JSFiddle: http://jsfiddle.net/rco9jvgg/

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