HTML DOM InputEvent Object


The InputEvent Object represents all the events that correspond to content change of a form element.

Properties

Here, “InputEvent” can have the following properties/methods −

Property/Method
Description
data
It returns the string corresponding to inserted character
dataTransfer
It returns an object containing information about the interested/deleted data
getTargetRanges()
Returns an array containing target ranges that will be affected by the insertion/deletion.
inputType
It returns the type of input
isComposing
It returns the state of the event

Example

Let us see an example for InputEvent data property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>InputEvent Data</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>InputEvent-Data</legend>
<label>Fill in the blanks:
<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';
      else
         divDisplay.textContent = 'Try Again, '+textSelect.placeholder;
      textSelect.textContent = '';
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before typing anything in the text field −

After typing wrong answer in the text field −

After typing correct answer in the text field −

Updated on: 30-Jul-2019

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements