简体   繁体   中英

How to display a Text in an input field on a button click?

i want any text like hello world to be appeared in a input field when i click the button. How can i do that?

enter code here

<button class='submit' onclick="myFunction()"> Click Me </button>
<input type= 'text' id='demo'>

Use eventListeners via addEventListener

 window.addEventListener("load", onLoadHandler); function onLoadHandler(){ let button = document.getElementById("myButton"); button.addEventListener("click",buttonClick); } function buttonClick(e){ let input = document.getElementById("textInput"); input.value = "Hello world;"; }
 <div> <input id="textInput"> </div> <div> <button id="myButton"> write in input </button> </div>

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