Add oninput attribute to HTML element with JavaScript?

For this, use the concept of document.getElementById() along with addEventListener(). Following is the JavaScript code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<label> Enter the value:</label>
<input id="enterValue" type="text">
   document.getElementById('enterValue').addEventListener("input", function () {
      console.log('you have entered the value');

To run the above program, save the file nameanyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.


When user enters a value −