EN
JavaScript - oninput event example
0 points
In this article, we would like to show you oninput
event example in JavaScript.
Quick solution:
xxxxxxxxxx
1
var myElement = document.querySelector('#my-element');
2
3
myElement.addEventListener('input', function() {
4
console.log('oninput event occurred.');
5
});
or:
xxxxxxxxxx
1
<input type="text" oninput="handleInput()">
or:
xxxxxxxxxx
1
var myElement = document.querySelector('#my-element');
2
3
myElement.oninput = function() {
4
console.log('oninput event occurred.');
5
};
There are three common ways how to use oninput
event:
- with event listener,
- with element attribute,
- with element property.
In this section, we want to show how to use oninput
event on input
element via event listener mechanism.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input type="text" id="my-input">
5
<script>
6
var myInput = document.querySelector('#my-input');
7
8
myInput.addEventListener('input', function() {
9
console.log('oninput event occurred.');
10
});
11
</script>
12
</body>
13
</html>
In this section, we want to show how to use oninput
event on input
element via attribute.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input type="text" oninput="handleInput()">
5
<script>
6
function handleInput(){
7
console.log('oninput event occurred.');
8
}
9
</script>
10
</body>
11
</html>
In this section, we want to show how to use oninput
event on input
element via property.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input type="text" id="my-input">
5
<script>
6
var myInput = document.querySelector('#my-input');
7
8
myInput.oninput = function() {
9
console.log('oninput event occurred.');
10
};
11
</script>
12
</body>
13
</html>