Button Click Alert

The basics of interactivity. This script demonstrates how to trigger a system dialog box when a user interacts with a button.

Copy the Script

<!-- Inline Method -->
<button onclick="alert('Hello World!')">Click Me</button>

<!-- Function Method (Cleaner) -->
<script>
function showMessage() {
    alert("This is a function-based alert.");
}
</script>
<button onclick="showMessage()">Run Function</button>

Frequently Asked Questions

No. The standard 'alert()' box is a system dialog controlled by the browser/OS. To style it, you must create a custom HTML modal (see our Custom Alert Box script).

Yes. You can add the 'onclick' attribute to any HTML tag, including <a>, <div>, or <img>.