Onpaste event in Javascript

Onpaste event Javascript

Sometimes, it may happen that we need to react if someone pastes something into the element on the webpage. In such situations, we can trigger the paste event. Here, we are going to use the HTML attribute for the event paste, which is Onpaste, and we would write a function, which would execute when the event is triggered. Let’s have a look at an example, where we would be trying to react to the paste event.

As you can clearly see in the code above, there is a silly question, and below that, there is a text area, which is given for writing the answer to the above silly question. But let’s say that someone pastes the answer onto the text area. In such a situation, the paste event would be triggered, the alert would be displayed, and the color of the text in the answer would become red.

You can try executing the above program. You can do something else as well after the event is triggered, but for example, we have just shown an alert and made the text of the answer red.

But you can do what the requirement says.