Để gọi một hàm khi nhấp vào nút trong JavaScript, hãy sử dụng onclick. Hãy thử chạy mã sau để gọi một hàm onclick trong JavaScript -
Ví dụ
<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>