user523234
user523234

Reputation: 14834

onpaste not sending pasted in value

I created a input field and using onpaste function to trigger the form submit. But it only submits the data currently in the input field if any not the pasted in data.

I need to send the pasted in data. Any suggestions what I did wrong?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        </head>
    <body>
        <form action="textDataSubmittedGet/" method="get" id="MyForm">
            Your Data: <input type="text" onpaste="myFunction()" name="textData"><br>
        </form>
    <script>
        function myFunction() {
            $("#MyForm").trigger('submit');
        }
    </script>
    </body>
</html>

enter image description here

Upvotes: 0

Views: 428

Answers (1)

Hacketo
Hacketo

Reputation: 4997

The problem is that you trigger the form before the value of the input is updated with the pasted content.

What you can do is to execute a function after the onpaste event has been executed.

To do so you can use setTimeout to execute the trigger on the next stack of execution right after onpaste

function myFunction() {
    setTimeout(function(){
        $("#MyForm").trigger('submit');
    }, 0 );
}

Upvotes: 1

Related Questions