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
.
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>
You could use onKeyPress/onKeyUp/onKeyDown/onPaste instead of onChange.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With