Content · Web Development · HTML

HTML error DOM Event: Reference and Examples

Learn when the error DOM event fires and how to handle it with JavaScript using a practical HTML example.

1 views Published Oct 19, 2026 Updated Oct 19, 2026 Admin

When someone interacts with a webpage, the browser sends events to the relevant JavaScript code. The error event happens when a resource or operation fails to load.

When error happens


A resource or operation fails to load.

You can listen for it using addEventListener(). That keeps event handling separate from HTML attributes such as onclick and works well with modern code organization.

A small working example


<img id="demo-target" alt="Sample photograph" width="240" height="160">
<p id="event-status" role="status">Waiting for error...</p>
<script>
    const eventTarget = document.getElementById("demo-target");
    const eventStatus = document.getElementById("event-status");

    if (eventTarget) {
        eventTarget.addEventListener("error", (eventDetails) => {
            eventStatus.textContent = "error event received";
            console.log(eventDetails.type, eventDetails.target);
        });
    }

    eventTarget.src = "missing-photo.jpg";
</script>

Reading the code


The browser listens for error on the relevant target. When the event occurs, the callback receives eventDetails, updates the status message and logs the event name and target in Developer Tools.

Request an image file that does not exist; error causes vary by target.

The event target identifies which resource failed. Load error details are often restricted for security.

Try it in your browser


Open the page, show the Console panel, and follow this test: Request an image file that does not exist; error causes vary by target. Then change one thing in the example and observe whether the event still fires.

Common Beginner Mistakes


  • Expecting error to fire without its required user action or browser condition.
  • Request an image file that does not exist; error causes vary by target.
  • Adding repeated event listeners without considering cleanup or unwanted side effects.

Key Takeaways


  • error reports a resource or operation fails to load.
  • addEventListener("error", ...) listens for the event on the appropriate target.
  • Request an image file that does not exist; error causes vary by target.
Share this resourceSend this page to someone who may find it useful.