HTML onclick Event Attribute

Example

Execute a JavaScript when the button is clicked:

<button onclick="copyText()">Copy Text</button>

Try It Yourself

Browser Support

IE Firefox Chrome Safari Opera

All major browsers support the onclick attribute.

Definition and Usage

The onclick attribute is triggered by a mouse click on the element.

Note:The onclick attribute is not applicable to the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> or <title>.

Differences between HTML 4.01 and HTML5

None.

Syntax

<element onclick="script">

Attribute Value

Value Description
script Script executed when onclick occurs.