HTML onbeforematch Event Attribute
Description
The onbeforematch event attribute is an HTML event handler that is triggered before a text match is highlighted in a document as part of a find-in-page operation (such as when a user uses Ctrl+F or the Find API). It allows developers to execute JavaScript just before the browser scrolls to or highlights a matching element.
This event is relatively new and is primarily supported in modern browsers like Chrome (and Chromium-based browsers). Its purpose is to give developers a chance to manipulate or modify content before it becomes a match, such as dynamically loading content or highlighting hidden elements.
Key Points
- Event Type:
Event(bubbles:true, cancelable:false) - Default Action: None. You cannot cancel the browser’s find behavior.
- Use Case: Useful when you have lazy-loaded content or hidden elements that should be revealed when a user searches for them.
How It Works
When a browser’s find operation identifies a potential match in the document:
- The
onbeforematchevent fires on the element containing the match. - You can execute JavaScript to make the element visible, load content, or apply styles.
- After your code runs, the browser continues with the default find-and-highlight action.
HTML Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>onbeforematch Example</title>
<style>
.hidden {
display: none;
background-color: yellow;
}
</style>
</head>
<body>
<p>This is a paragraph with some searchable text.</p>
<div id="lazy-content" class="hidden" onbeforematch="this.style.display='block';">
This content was hidden but will appear if searched.
</div>
<p>Try searching for the word "hidden" in this page using Ctrl+F.</p>
</body>
</html>
Explanation:
- The
divwithid="lazy-content"is initially hidden. - When a user searches for
"hidden"using the browser’s find tool, theonbeforematchevent triggers. - The inline JavaScript sets
display='block', revealing the hidden content so the browser can highlight it.
JavaScript Example
You can also attach it programmatically:
const lazyDiv = document.getElementById('lazy-content');
lazyDiv.onbeforematch = () => {
lazyDiv.style.display = 'block';
};
Typical Use Cases
- Lazy-loading content: Load hidden sections of the page only when a search target exists.
- Dynamic styling: Highlight elements in a special way when they are about to be matched.
- Expandable menus or accordions: Automatically open hidden panels if they contain the search text.
Syntax
<div hidden="until-found" onbeforematch="script(event)">This content is hidden but searchable.</div>
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML onbeforematch event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
This event attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 27th December 2025
