Input value always null on `onkeydown` or `onkeypress` event in Blazor
See original GitHub issueInput value always null on onkeydown
or onkeypress
event but have value on onclick
event in Blazor.
Here is my code:
<input onkeydown="@CheckInput" placeholder="Write To Do" bind="@NewToDo" />
<button onclick="@AddToDo">Add todo</button>
<br />
<span class="text-danger">@errorMessage</span>
@functions {
List<TodoItem> todos = new List<TodoItem>();
private string NewToDo { get; set; }
private string errorMessage;
private void CheckInput()
{
Console.WriteLine(NewToDo); // Strangely input value always null here.
}
private void AddToDo()
{
if (!string.IsNullOrWhiteSpace(NewToDo)) // Here value is getting as expected
{
errorMessage = null;
todos.Add(new TodoItem() { Title = NewToDo, IsDone = false });
NewToDo = null;
}
else
{
errorMessage = "Please enter a To do item!";
}
}
}
Why I am getting input field value always null on onkeydown
or onkeypress
while getting the value on onclick
?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
The @onkeypress doesn't get the updated value of Text ...
I want to use Rx to throttle the key press so less database calls will be invoked. Is there a way to to...
Read more >How to read the current value of an input in ...
To read the current value of an input using @onkeypress event, this event uses the KeyboardEventArgs. You can also get the last pressed...
Read more >Problem with onkeypress event
I have problem with following code - trying to change Value to static value ... but with more keys, event is triggered, but...
Read more >Blazor input onkeydown - how to remove a specific ...
I think the problem is in the CurrentValue because when I enter " e " the CurrentValue becomes null. When i enter your...
Read more >Event Handling In Blazor
The value of the attribute is treated as an event handler. Following is Razor syntax to define the event in the Blazor component,....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Try onkeydown=“@((__value) => NewToDo = __value)”
Read my comment here: https://github.com/aspnet/AspNetCore/issues/6052#issuecomment-450139089