Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an onChange event was called by an onPaste

I have a Javascript function that is associated with an onChange event. I understand that some browsers support onPaste at either the document or an element level. Is there a way to determine if the onChange event was caused by a "paste" - I tried adding a global var that gets set when onPaste is fired, and then reset it at the end of my onChange routine, but there is no guarantee that the onPaste function gets called before the onChange.

like image 797
JJSO Avatar asked Nov 05 '22 07:11

JJSO


2 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>
like image 122
Shadow Wizard Hates Omicron Avatar answered Nov 11 '22 03:11

Shadow Wizard Hates Omicron


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

like image 40
Raphael Michel Avatar answered Nov 11 '22 05:11

Raphael Michel