HTML global attributeGlobal attributes

onclick

Runs JavaScript code when an element is clicked. The simplest, but not the best way to handle events.

Applies to
All HTML elements
On this page

Example

#
HTML
<button type="button" onclick="this.textContent = 'Enrolled in the course'; this.disabled = true">
  Enroll in the HTML course
</button>

<p>Courses in the cart: <strong id="cart">0</strong></p>
<button type="button" onclick="
  const cart = document.getElementById('cart');
  cart.textContent = Number(cart.textContent) + 1;
">Add a course to the cart</button>
Preview

In the first button, this points to the button itself, so the code changes its label and disables it. The second button finds the counter by id and increases it on every click. Move to the buttons with the Tab key and press Enter: that is a click too.

Definition and usage

#

The onclick attribute contains JavaScript code that the browser runs when the element is clicked. The click event fires not only after a mouse click or a screen tap: a button fires it after Enter or Space is pressed too, and a link after Enter. So on a <button> element, one attribute handles mouse, touch and keyboard.

The browser wraps the attribute’s code in a function with an event parameter. Inside, you have this, the element the attribute sits on, and event, the event object. event.target points to the element that was actually clicked. It may be a child of the element with the attribute, because the click travels up the tree (bubbling). Returning false cancels the default action, just like event.preventDefault().

Event attributes are handy in short examples, but in real projects it is better to use the addEventListener method in a script. The HTML then stays clean, one element can have several handlers, and the page also works with a strict Content Security Policy (CSP), which blocks code written in attributes. The attribute sees only global functions, so you cannot call a function declared in a <script type="module"> from it.

Also beware of an unusual way of looking up names. In the attribute’s code, the browser checks the element’s own properties first, then the form the element belongs to, then the document object, and only at the end global variables. That is why your own open() or clear() function called from the attribute will not run. document.open() runs instead, which wipes the whole page, or document.clear(), which does nothing.

Syntax

#
Syntax
<element onclick="JavaScript code">…</element>

Available in the attribute’s code

#
  • this

    The element the attribute sits on. Through this you can change its text, classes or attributes.
  • event

    The event object. For a click it contains, among other things, the cursor position (clientX, clientY) and whether Shift or Ctrl was held (shiftKey, ctrlKey).
  • event.target

    The element that was actually clicked. It may be a child of the element with the attribute.
  • event.preventDefault()

    Cancels the default action, for example following a link’s address or toggling a checkbox.
  • return false

    In an attribute it works like event.preventDefault(). In a function added with addEventListener it has no such effect.

More examples

#
The difference between this and event.target
HTML
<ul onclick="
  document.getElementById('log').textContent =
    'this: ' + this.tagName + ', event.target: ' + event.target.tagName;
">
  <li>Lesson 1: <strong>tags</strong></li>
  <li>Lesson 2: <em>attributes</em></li>
</ul>
<p id="log">Click the list, preferably on a bold or italic word.</p>
Preview

The attribute sits on the <ul> list, so this always points to the list. event.target is the element the cursor hit: LI, STRONG, EM, and sometimes the list itself. Event delegation is built on this: one handler for many elements.

The same with addEventListener
HTML
<button type="button" id="enroll">Enroll in the HTML course</button>

<script>
  const button = document.getElementById('enroll');

  button.addEventListener('click', (event) => {
    event.currentTarget.textContent = 'Enrolled in the course';
    event.currentTarget.disabled = true;
  });
</script>
Preview

This is how events are handled in real projects. In an arrow function this does not point to the element, so you use event.currentTarget instead.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • The clickable thing should be a <button> or an <a>, not a <div> with an onclick attribute. You cannot reach a <div> with the Tab key, and a screen reader will not say it can be clicked.
  • In React, the onClick={handleClick} notation only resembles an HTML attribute. You pass a function, not a string with code, and it is React that adds the event listener.
  • Do not put long code in the attribute. If you need more than one or two statements, move it into a function in a script, and preferably straight into addEventListener.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.