How to use EventListner In Javascript

An EventListener in JavaScript waits for a specified event, like a click or key press, on a DOM element and triggers a function to execute in response to that event.

Syntax:

const inputField = document.getElementById('email'); // fetch Id of Inputfield
inputField.addEventListener('paste', e => e.preventDefault());

Example: Below is an example of disabling ctrl+v in javascript using EventListner.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <h2>Text To Copy</h2>

    <label>CRTL V Disabled </label><br>
    <input type="text" id="email" required> <br><br>

    <label>CRTL V Enabled </label><br>
    <input type="text" id="pass" required>

    <script>
        const inputField = 
              document.getElementById('email');
        inputField.addEventListener('paste', e => e.preventDefault());
    </script>
</body>

</html>

Output:

Disable ctrl+v in javascript using EventListner



How to Disable Ctrl+V (Paste) in JavaScript?

Similar Reads

What is Ctrl + V ?

The ctrl+V is a keyboard shortcut used to paste anything from anywhere. It can be disabled for a particular task or page. Let’s see how to disable cut, copy, paste, and right-click....

Using JQuery

JQuery is a fast, small, and feature-rich JavaScript library. It simplifies tasks like HTML document traversal and manipulation, event handling, animation, and AJAX....

Using EventListner

An EventListener in JavaScript waits for a specified event, like a click or key press, on a DOM element and triggers a function to execute in response to that event....