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