question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input value always null on `onkeydown` or `onkeypress` event in Blazor

See original GitHub issue

Input 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:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Boglencommented, Dec 24, 2018

Try onkeydown=“@((__value) => NewToDo = __value)”

1reaction
Andrzej-Wcommented, Dec 27, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found