หากต้องการเรียกใช้ฟังก์ชันเมื่อคลิกปุ่มใน JavaScript ให้ใช้ onclick ลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียกใช้ฟังก์ชัน onclick ใน JavaScript -
ตัวอย่าง
<html>
<head>
<script>
function sayHello() {
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>