[英]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.当我点击按钮时,我希望任何像 hello world 这样的文本出现在输入字段中。 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
通过addEventListener
使用 eventListeners
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.