ใช้ oninput แอตทริบิวต์เหตุการณ์ที่จะทริกเกอร์เมื่อองค์ประกอบได้รับการป้อนข้อมูลของผู้ใช้ คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อใช้งาน oninput แอตทริบิวต์ −
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<p>Write your name below:</p>
<input type = "text" id = "myid" oninput="display()">
<p id="test"></p>
<script>
function display() {
var p = document.getElementById("myid").value;
document.getElementById("test").innerHTML = "Your answer is " + p;
}
</script>
</body>
</html>