whatwg / whatwg/html

Add a Generic "onrender" or "onattach" Event for HTML Elements

Open
#11,611 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

addition/proposal needs implementer interest
Dominant language
HTML
Stars
9.4k
Forks
3.2k
Avg merge
3d 9h
Merged PRs (30d)
38

Description

What problem are you trying to solve?

Currently, there is no native way in HTML to detect when a generic container element (such as <div>, <section>, etc.) has been rendered and attached to the DOM, and its layout has been calculated.

This limitation makes it difficult to implement UI patterns that require knowledge of the element’s rendered size or need to trigger logic immediately after the element is available in the DOM (e.g., for dynamic transitions, collapsible panels, or responsive layouts).

What solutions exist today?

Developers are forced to use workarounds such as:

  • Using <img src="404" onerror="..."> to trigger logic after rendering (hacky and not semantic)
  • Relying on JavaScript frameworks or MutationObservers (adds complexity and overhead)
  • Setting arbitrary large values for CSS properties like max-height (not robust or accessible)
  • Using global scripts to loop through all matching elements after page load (inefficient for dynamic content)
How would you solve it?

A generic event for HTML elements, such as onrender or onattach, which fires when:

  • The element is attached to the DOM
  • Its layout and dimensions are available (i.e., after rendering and style calculation)

This event would allow developers to run logic (e.g., set dynamic styles, initialize transitions, measure content) in a reliable and declarative way, without hacks or global scripts.

Anything else?

Example:

<div onrender="initCollapse(this)">
  <!-- content -->
</div>

Or as an event listener:

element.addEventListener('render', function(e) {
  // Element is now in the DOM and layout is available
});

Benefits

  • Enables robust UI patterns for dynamic content and transitions
  • Reduces reliance on hacks and global scripts
  • Improves accessibility and maintainability
  • Makes it more declarative and developer-friendly

Use Cases

  • Collapsible panels with animated transitions based on content height
  • Responsive components that need to measure their own size
  • Widgets that initialize only after being attached to the DOM
  • Any scenario where layout-dependent logic is needed immediately after rendering

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the HTML Standard's existing DOM attachment, event dispatch, and rendering-related mechanisms. Compare the proposed onrender/onattach behavior with current browser APIs and identify the timing, layout, and dynamic-content semantics that would need specification. Done means a precise, implementable event proposal with resolved lifecycle behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
html
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.