Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
HTML DOM InputEvent Object
The InputEvent Object in the HTML DOM represents events that occur when the content of a form element changes. This includes typing text, deleting content, pasting data, or any other modification to input elements like text fields, textareas, and content-editable elements.
The InputEvent interface extends the UIEvent interface and provides detailed information about the type of input change that occurred, making it useful for creating responsive user interfaces and real-time validation.
Properties and Methods
The InputEvent object provides the following properties and methods −
| Property/Method | Description |
|---|---|
| data | Returns the string containing the inserted characters, or null for deletions |
| dataTransfer | Returns a DataTransfer object containing information about inserted/deleted data |
| getTargetRanges() | Returns an array of StaticRange objects representing the ranges affected by the input |
| inputType | Returns a string describing the type of input (e.g., "insertText", "deleteContentBackward") |
| isComposing | Returns a boolean indicating if the event is part of a composition session |
Common Input Types
The inputType property can return various values indicating the nature of the input change −
- insertText − Regular text insertion
- insertCompositionText − Text inserted via input method composition
- deleteContentBackward − Content deleted via backspace
- deleteContentForward − Content deleted via delete key
- insertFromPaste − Content inserted via paste operation
Example − InputEvent Data Property
Following example demonstrates the usage of the InputEvent data property to detect specific character input −
<!DOCTYPE html>
<html>
<head>
<title>InputEvent Data Property</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
form { width: 70%; margin: 0 auto; text-align: center; }
fieldset { padding: 20px; border: 2px solid #ddd; border-radius: 10px; }
input[type="text"] { padding: 8px; margin: 10px; font-size: 16px; }
#divDisplay { margin-top: 15px; font-weight: bold; min-height: 20px; }
.correct { color: green; }
.incorrect { color: red; }
</style>
</head>
<body>
<form>
<fieldset>
<legend>InputEvent Data Detection</legend>
<label>Fill in the blank: <input type="text" id="textSelect" placeholder="__ for Apple" oninput="getEventData(event)"></label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var textSelect = document.getElementById("textSelect");
function getEventData(inputEvent) {
if (inputEvent.data === 'A') {
divDisplay.textContent = 'Correct Answer!';
divDisplay.className = 'correct';
} else if (inputEvent.data !== null) {
divDisplay.textContent = 'Try Again: ' + textSelect.placeholder;
divDisplay.className = 'incorrect';
}
}
</script>
</body>
</html>
This example checks if the user types the letter 'A' and provides immediate feedback. The inputEvent.data contains the character that was just inserted.
Example − InputEvent Type Detection
Following example shows how to detect different types of input operations using the inputType property −
<!DOCTYPE html>
<html>
<head>
<title>InputEvent Type Detection</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.container { max-width: 600px; margin: 0 auto; }
textarea { width: 100%; height: 100px; padding: 10px; font-size: 14px; }
#output { margin-top: 15px; padding: 10px; background-color: #f5f5f5; border-radius: 5px; }
.log-entry { margin: 5px 0; padding: 5px; border-left: 3px solid #007bff; }
</style>
</head>
<body>
<div class="container">
<h2>Input Type Logger</h2>
<textarea id="inputArea" placeholder="Type, delete, or paste content here..."></textarea>
<div id="output"><strong>Input Log:</strong></div>
</div>
<script>
var inputArea = document.getElementById('inputArea');
var output = document.getElementById('output');
var logCount = 0;
inputArea.addEventListener('input', function(event) {
logCount++;
var logEntry = document.createElement('div');
logEntry.className = 'log-entry';
logEntry.innerHTML = logCount + '. Type: ' + event.inputType +
', Data: ' + (event.data || 'null') +
', Composing: ' + event.isComposing;
output.appendChild(logEntry);
// Keep only last 5 entries
if (output.children.length > 6) {
output.removeChild(output.children[1]);
}
});
</script>
</body>
</html>
This example logs different types of input operations, showing the inputType, data, and isComposing properties in real-time as the user interacts with the textarea.
Example − Composition Events
Following example demonstrates how the isComposing property works with input method composition −
<!DOCTYPE html>
<html>
<head>
<title>InputEvent Composition</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.container { max-width: 500px; margin: 0 auto; }
input { width: 100%; padding: 12px; font-size: 16px; margin: 10px 0; }
.status { padding: 10px; margin: 10px 0; border-radius: 5px; }
.composing { background-color: #fff3cd; border: 1px solid #ffeaa7; }
.completed { background-color: #d4edda; border: 1px solid #c3e6cb; }
</style>
</head>
<body>
<div class="container">
<h2>Composition Status Monitor</h2>
<p>Try typing with an input method (e.g., Chinese, Japanese, or Korean characters):</p>
<input type="text" id="composeInput" placeholder="Type here...">
<div id="status" class="status">Ready to type</div>
</div>
<script>
var composeInput = document.getElementById('composeInput');
var status = document.getElementById('status');
composeInput.addEventListener('input', function(event) {
if (event.isComposing) {
status.textContent = 'Composing: ' + (event.data || 'in progress...');
status.className = 'status composing';
} else {
status.textContent = 'Completed input: ' + (event.data || 'finished');
status.className = 'status completed';
}
});
</script>
</body>
</html>
This example shows how isComposing helps distinguish between intermediate composition states and final character input, which is useful for international text input.
Browser Support and Usage
The InputEvent interface is supported in all modern browsers. However, some properties like dataTransfer and getTargetRanges() have limited support. The data, inputType, and isComposing properties are widely supported and safe to use in production applications.
InputEvent is particularly useful for −
- Real-time input validation and formatting
- Character counting and text analysis
- Implementing rich text editors
- Detecting paste operations and clipboard content
- Supporting international input methods
Conclusion
The InputEvent Object provides detailed information about content changes in form elements, enabling developers to create responsive and interactive user interfaces. Its properties like data, inputType, and isComposing offer precise control over input handling, making it essential for modern web applications that require real-time text processing and validation.
