DA.
DA.

Reputation: 40673

Getting iPhone GO button to submit form

Is anyone aware of what variables go into a form to make the iPhones virtual keyboard's GO button submit the form vs. not?

I've been trying to narrow down the scenarios and this is what I've found:

However, that's not completely accurate, as we're running into a situation the 3rd isn't working for us.

Is anyone aware of the other factors that go into the GO button working vs. not working?

Upvotes: 44

Views: 48270

Answers (9)

Tim Hettler
Tim Hettler

Reputation: 1256

As of writing (11/19/2019), on iOS 13.1.3, the following scenarios change the return key label:

  • The INPUT element must be wrapped in a FORM element, and the FORM element must have an action attribute. (It can be an empty string, i.e. action="".)
  • If the INPUT element has a type of search, the return key have a label of search.
  • Any other value for the type attribute will result in the return key having a label of go.

The presence of an INPUT or BUTTON element with a type set to submit does not appear to affect the return key label. (Though it's a good idea to include one for accessibility purposes.)

Upvotes: 1

Sachintha Udara
Sachintha Udara

Reputation: 701

GO button to submit is a default behaviour of iOS and don`t try to hack the keyboard because UIKeyboard is runtime header, however you can inject JS for your html in runtime and prevent GO button behaviour (GO acts like a Enter key),

Try this,

    WKWebView *webView;  
    WKUserContentController *contentController = [[WKUserContentController alloc] init];

    NSString *script1 = @"var isEnter = false;";
    WKUserScript *userScript1 = [[WKUserScript alloc] initWithSource:script1 injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:false];
    [contentController addUserScript:userScript1];

    NSString *script2 = @"function captureGoKey(e){if(isEnter){e.preventDefault();}isEnter = false;}";
    WKUserScript *userScript2 = [[WKUserScript alloc] initWithSource:script2 injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:false];
    [contentController addUserScript:userScript2];

    NSString *script3 = @"var form = document.getElementsByTagName('form')[0];";
    WKUserScript *userScript3 = [[WKUserScript alloc] initWithSource:script3 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
    [contentController addUserScript:userScript3];

    NSString *script4 = @"document.onkeypress = function(e){if(e.keyCode == 13){isEnter = true;}}";
    WKUserScript *userScript4 = [[WKUserScript alloc] initWithSource:script4 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
    [contentController addUserScript:userScript4];

    NSString *script5 = @"if(form.attachEvent){form.attachEvent('submit', captureGoKey);}else{form.addEventListener('submit', captureGoKey);}";
    WKUserScript *userScript5 = [[WKUserScript alloc] initWithSource:script5 injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false];
    [contentController addUserScript:userScript5];

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = contentController;

    webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];

Upvotes: 0

Jonny Forney
Jonny Forney

Reputation: 2078

The code given by the others is correct. If you are using jQuery Mobile then add

data-role="none" 

to the submit input element. Like so:

<input type="submit" data-role="none" style="visibility: hidden; position: absolute;" />

Upvotes: 1

David Merritt
David Merritt

Reputation: 147

You can also bind a keypress listener to the element or form. The iphone "Go" button is the same as the enter button on a computer, char 13.

$('someElem').bind("keypress", function(e){
    // 'Go' key code is 13
    if (e.which === 13) {
       console.log("user pressed Go");
       // submit your form with explicit JS.
    } 
 });

Upvotes: 4

Nathan Todd
Nathan Todd

Reputation: 83

I could not work out why a very simple google maps form was not submitting using the iPhone Go button.

Turns out, after stripping it down, it does not work with target="_blank" on the form tag.

Removed that, and now the Go button works on iPhone.

Here's a JSFiddle to try it

Upvotes: 3

pragman
pragman

Reputation: 1644

Here's the submit button style that worked for me, with minimum side-effects on the page:

.... style="width: 0px ; height: 0px" ....

Upvotes: 0

Jiri Kopsa
Jiri Kopsa

Reputation: 521

If there are more than one inputs and you want to hide the submit, the best seems:

<input type="submit" style="visibility:hidden;position:absolute"/>

Upvotes: 23

DA.
DA.

Reputation: 40673

So, here was our specific issue:

We had a form with multiple user input fields, but not a true <input type="submit"> (it was being submitted via JS).

As such, the GO button did nothing.

We then added an <input type="submit"> and set it to display: none hoping that would do the trick. Nope. Didn't work.

On a whim, we changed display: none to margin-left: -1000px

That worked!

Apparently, Safari is looking for the presence of a SUBMIT button in the form and only if it's not display: none, it will then fire it when you hit the GO button.

Upvotes: 56

Oz Lodriguez
Oz Lodriguez

Reputation: 973

Just enclosing my input type='search' in a form tag did the trick when I encountered this problem. Hopefully it might help others that had this problem as well.

Upvotes: 0

Related Questions