Gán thuộc tính onclick="functionName()" cho button HTML, định nghĩa hàm trong script.
Mã HTML/JS
<!DOCTYPE html> ... <body>
<p id="showTheTextMessage"></p>
<button onclick="displayingMessageOnButtonClick()">Press Me</button>
</body>
<script>
function displayingMessageOnButtonClick() {
const el = document.getElementById('showTheTextMessage');
el.innerHTML = 'Welcome to Javascript Program...';
el.style.display = 'block';
}
</script>
</html>
Kết quả
Nhấn button → hiển thị dòng chữ "Welcome to Javascript Program..."