Languages
[Edit]
EN

JavaScript - oninput event example

0 points
Created by:
Dirask Community
6300

In this article, we would like to show you oninput event example in JavaScript.

Quick solution:

var myElement = document.querySelector('#my-element');

myElement.addEventListener('input', function() {
    console.log('oninput event occurred.');
});

or:

<input type="text" oninput="handleInput()">

or:

var myElement = document.querySelector('#my-element');

myElement.oninput = function() {
    console.log('oninput event occurred.');
};

 

Practical examples

There are three common ways how to use oninput event:

  • with event listener,
  • with element¬†attribute,
  • with element property.

1. Event listener based example

In this section, we want to show how to use oninput event on input element via event listener mechanism.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
  <body>
    <input type="text" id="my-input">
    <script>
      var myInput = document.querySelector('#my-input');

      myInput.addEventListener('input', function() {
        console.log('oninput event occurred.');
      });
    </script>
  </body>
</html>

2. Attribute based example

In this section, we want to show how to use oninput event on input element via attribute.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
  <body>
    <input type="text" oninput="handleInput()">
    <script>
      function handleInput(){
        console.log('oninput event occurred.');
      }
    </script>
  </body>
</html>

3. Property based example

In this section, we want to show how to use oninput event on input element via property.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
  <body>
    <input type="text" id="my-input">
    <script>
      var myInput = document.querySelector('#my-input');

      myInput.oninput = function() {
        console.log('oninput event occurred.');
      };
    </script>
  </body>
</html>

JavaScript - events

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join