HTML onclick event attribute

Example

Execute a JavaScript when clicking the button:

<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 mouse clicks 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.