代表者の戯言

insert something on the textbox



Although it's a fundamental concept in JavaScript, if you want to display something in a text box, please refer to the following program. One key point of this program is that when using a function, event.preventDefault(); is not necessary. However, if you write it plainly (i.e., without wrapping it in a function), this event becomes necessary.


<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>textoxに表記する</title>

<link rel="stylesheet" href="reset.css">

<link rel="stylesheet" href="main2.css">

</head>

<body>

<div class="container">

<form>

<p><input type="text" id="t1" placeholder="ここに表示"></p>

<p> <button id="button1">ボタン1</button></p>

<p><input type="button" value="ボタン2" onclick="disp()"></p>

<script>

document.getElementById("button1").addEventListener("click", function () {

event.preventDefault();

document.getElementById("t1").value = "Hello";

});

function disp() {

document.getElementById("t1").value = "function利用";

}

</script>

</form>

</div>

</body>

</html>

VB