How do I conditionally PreventDefault inside the input component in Blazor?

Platform: Blazor| Category: Forms and validation

You can use the “preventDefault” attribute to prevent certain actions(events). In the following example, input keypress event checks whether the key value is “a” to prevent the keypress event. 

<input value="@name" type="text" @onkeydown="@onKeydown" @onkeydown:preventDefault="@isPreventKey" />

@code {

    private string name { get; set; }

    private bool isPreventKey { get; set; }

    private void onKeydown(KeyboardEventArgs args)
    {
        if(args.Key == "a")
        {
            this.isPreventKey = true;
        }
    }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.