Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change variable value on input key press on Blazor

What I want to do is update the variable value when the user press a key, but it only update the value on blur of the input.

The following code is not working.

<p>@increment</p>
<input 
    type="text"
    @onchange="@((ChangeEventArgs e) =>
        increment = e.Value.ToString())"
/>

@code {
    string increment;
}

Using @bind and @bind-value also doesn't work.

I made a blazorfiddle with the example.

Who can I make the value of my variable to change when the a key is pressed?

like image 972
Vencovsky Avatar asked Oct 03 '19 15:10

Vencovsky


People also ask

How do you get the input field value in Blazor?

The value of an input element is updated in the wrapper with the change events of elements in Blazor. To get the current value for each character input, you must use the oninput event of the input element. This can be achieved by binding the oninput event (native event) using the @bind:event=“oninput“.

How do you change input value?

Use input change event to get the changed value in onchange event argument. If you bind using the two-way bind to value property, it will automatically change the value into the value property.

What is the difference between bind and bind value in Blazor?

In Blazor, there is no significant difference between using these two attributes. The @bind attribute is a shorthand of the @bind-value attribute and its delegate will trigger the ValueChanged event of the component.

How do you bind a label to value in Blazor?

You can use the bind attribute on any element to bind the value. In blazor, we'll have a property assigned some value in the functions and use the property in the HTML template. Let's get this done. So, when we run the app, the label tag will display “red” as a text in the label.


2 Answers

Answer:

Quoting Data Binding docs:

<input @bind="CurrentValue" 
       @bind:event="oninput" />

Unlike onchange, which fires when the element loses focus, oninput fires when the value of the text box changes.

Using @oninput:

You can achieve it without @bind directive:

<input value="@CurrentValue"
       @oninput="(e)=> CurrentValue = e.Value.ToString()"/>

InputText & oninput

For people looking for oninput on InputText component the answer is full documented on InputText based on the input event doc:

Use the InputText component to create a custom component that uses the input event instead of the change event. Shared/CustomInputText.razor:

@inherits InputText

<input @attributes="AdditionalAttributes" class="@CssClass" 
    @bind="CurrentValueAsString" @bind:event="oninput" />

The CustomInputText component can be used anywhere InputText is used:

<CustomInputText @bind-Value="exampleModel.Name" />
like image 123
dani herrera Avatar answered Sep 18 '22 03:09

dani herrera


In case anyone wants to do this with the InputText form component provided create your own component file, eg InputTextOnInput.razor:

@inherits InputText

<input @attributes="AdditionalAttributes" class="@CssClass" 
   @bind="CurrentValueAsString" @bind:event="oninput" />

Then use it in your form like so:

<InputTextOnInput @bind-Value="@PropertyToUpdate " />

@code {
    private string PropertyToUpdate { get; set; }
}

More details on the Blazor docs

like image 35
Luke Avatar answered Sep 18 '22 03:09

Luke