Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit with enter key

Tags:

f#

websharper

Is there an idiomatic way for me trigger my formlet's submit action when a keydown event is pressed?

Should I drop back down to DOM manipulation, or is there some Enhancement that I can use?

like image 692
Khanzor Avatar asked Jun 11 '26 11:06

Khanzor


1 Answers

Unfortunately, at the moment there is no standard way to do this. We do intend to add it in a future version though, either as an Enhance combinator or as a new option to Enhance.WithCustomSubmit*.

We actually encountered the same problem when creating FPish, and we use the following workaround:

[<JavaScript>]
let TriggerOnEnter (formlet : Formlet<'T>) =
    formlet
    |> Formlet.MapElement (fun elem ->
        let e = JQuery.JQuery.Of(elem.Body)
        e.Keypress(fun _ k ->
            // Opera uses charCode
            if k?keyCode = 13 || k?charCode = 13 then
                JavaScript.SetTimeout (fun _ ->
                    e.Find("input[type=button]").Trigger("click").Ignore
                ) 100 |> ignore
            k.StopPropagation()
        ).Ignore
        elem
    )

Note that it triggers the first button in the form, so you might need adjustments to the jQuery selector to make it actually trigger the submit button.

like image 154
Tarmil Avatar answered Jun 15 '26 05:06

Tarmil