1

A data entry person is putting email address into a form and it would be helpful if the form submitted each time he pasted in an address so it would be ready for the next paste. Other code within the setTimeout also works fine--just not the form submission. Thanks in advance for your help.

$("#email").on("paste", function(){
    setTimeout(function () {
        $("#cancelemail").submit();
    }, 0);
});


<form action="done.php" method="post" id="cancelemail">

Address to be removed: <input id="email" name="email" type="text" />

<input type="submit" name="submit" value="Remove">

</form>
karthikr
  • 97,368
  • 26
  • 197
  • 188
Mike C
  • 79
  • 1
  • 6
  • @JPod jquery has a 'paste' event – Milche Patern Nov 27 '13 at 18:49
  • 1
    Rather than using the `paste -> browser-submit -> reload page` logic, would you find it more friendly to use this logic? `paste -> AJAX submit -> clear input -> maintain focus on input -> display small success/error message below input box` – MonkeyZeus Nov 27 '13 at 18:54
  • Is it possible that a set timeout of 0 is just too fast? What happens if you up the set timeout to just one second? – jonnybot Nov 27 '13 at 18:57
  • @jonnybot "just one second" its quite a lot, one second IMHO – A. Wolff Nov 27 '13 at 19:01
  • Source : http://stackoverflow.com/questions/12447045/how-to-auto-submit-form-after-entering-text-of-certain-length-into-input-field $('yourTextBoxSelector').on("change paste keyup", function(){ if($(this).val().length >15){ $('yourFormSelector').submit() } }); But Hey, you should try search for it [over the net](https://www.google.ca/search?newwindow=1&site=&source=hp&q=jQuery:%20Submit%20form%20on%20paste). – Milche Patern Nov 27 '13 at 18:47

1 Answers1

3

That's strange, triggering the submit doesn't seem to be working for me unless I remove the submit button from the HTML, but triggering the submit button actually does ?

$("#email").on("paste", function () {
    setTimeout(function() {
        $('[name=submit]').trigger('click')
    });
});

Fiddle with button, triggering submit -> not working ?
Fiddle without button, triggering submit -> works
Fiddle with button, triggering button click -> works

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @A.Wolff - Thanks for the link, now that I've read that, I realize that I already knew that, but just couldn't for the life of me figure out why it wouldn't submit, and now it seems so obvious. It's one of those mistakes that I never I do myself, and you don't see it very often, but naming an element "submit" will indeed overwrite the native submit function attached to the form. – adeneo Nov 27 '13 at 19:10
  • @A.Wolff: Your comment was exactly the problem with my original code too. Thanks for leading us there adeneo. – Mike C Nov 27 '13 at 23:44