How can you read the current value of an input in an onkeypress method in Blazor?

stovroz picture stovroz · Oct 4, 2018 · Viewed 10.7k times · Source

I have:

<input onkeypress="@Foo" />

and:

@functions
{
    void Foo(UIKeyboardEventArgs e)
    {

    }
}

How do I pass, or otherwise retrieve, the value of the input in the handler method?

Answer

czlatea picture czlatea · Apr 27, 2019

Unfortunately, you need oninput event to update the bound variable.

@value
<input type="text" @bind="@value" @oninput="@(ui => { value = (string) ui.Value);})" />

@functions {
    string value;
}