CKTIS / HTML
onbeforeinput — handling an event before editing
onbeforeinput is an event-handler attribute for beforeinput, which concerns user editing of data. It lets JavaScript run before a change and cancel the default operation when the event is cancelable.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
onbeforeinput="JavaScript statements"
element.onbeforeinput = function (event) { /* ... */ };onbeforeinput is an HTML attribute containing handler code for the beforeinput event. It is available on all HTML elements; adding it does not itself make an element editable. Its JavaScript counterpart is the element.onbeforeinput property.
Inside the attribute code, event refers to the current event. Calling event.preventDefault() can block the default operation only when event.cancelable is true. Do not assume that every change can be stopped this way.
The attribute value is JavaScript code, not an event name. The example checks handling and cancellation of a synthetically dispatched beforeinput; it does not simulate actual typing. The unchanged value ABC therefore does not prove that native editing was blocked.
Examples
<!doctype html>
<meta charset="utf-8">
<title>onbeforeinput</title>
<input id="editor" value="ABC" onbeforeinput="if (event.cancelable) event.preventDefault()">
<pre id="result"></pre>
<script>
const editor = document.getElementById('editor');
const event = new Event('beforeinput', { bubbles: true, cancelable: true });
const allowed = editor.dispatchEvent(event);
document.getElementById('result').textContent =
`allowed=${allowed}\ndefaultPrevented=${event.defaultPrevented}\nvalue=${editor.value}`;
</script>Result
allowed=false defaultPrevented=true value=ABC
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.