How to determine if an onChange event has been triggered by onPaste

I have a Javascript function associated with the onChange event. I understand that some browsers support onPaste both at the document level and at the element level. Is there a way to determine if the onChange event was triggered by an "insert" - I tried to add a global var, which is set when onPaste starts, and then reset at the end of my onChange procedure, but there is no that the onPaste function is called before onChange.

+3
source share
4 answers

This worked fine for me:

<input type="text" onchange="ValueChanged(event, this);" onpaste="this.setAttribute('pasted', '1');"/>


<script type="text/javascript">
function ValueChanged(evt, sender) {
    var blnCameFromPaste = ((sender.getAttribute("pasted") || "") == "1");
    if (blnCameFromPaste)
        alert("changed by paste");
    else
        alert("changed without paste");
    sender.setAttribute("pasted", "0")
}
</script>
+1
source

You can use onKeyPress / onKeyUp / onKeyDown / onPaste instead of onChange.

+1

, . . .

One way to detect paste is to count the time between β€œkeystrokes” after the field has focus.

0
source

Each time you see the onchange event, compare the current length of the field value with the previous length and the current time to the previous time. If characters were entered faster than a person could enter them, they must have been inserted.

0
source

Source: https://habr.com/ru/post/1773088/


All Articles