Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor, how can I trigger the enter key event to action a button function?

$
0
0

I was trying the to-do list example from Microsoft.

I want to add a todo item, but instead of pressing the button with a mouse click I want to press the enter key. I'm not happy with using JS like in this solution: How to set the focus to an InputText element?I tried to trigger the method private void Enter(KeyboardEventArgs e) by this line of code:

<button @onclick="AddTodo" @onkeypress="@(e=>Enter(e)" tabindex="0"  >Add todo</button>

However, it didn't work.

    enter code here<input placeholder="Something todo" @bind="newTodo" /><button @onclick="AddTodo" @onkeypress="Enter" tabindex="0"  >Add todo</button>    @code {        private IList<TodoItem> todos = new List<TodoItem>();        private string newTodo;        private void AddTodo()        {            if (!string.IsNullOrWhiteSpace(newTodo))            {                todos.Add(new TodoItem { Title = newTodo });                newTodo = string.Empty;            }        }        //private void Enter(KeyboardEventArgs e)            private void Enter()        {            //if (e.Key == "Enter")            {                if (!string.IsNullOrWhiteSpace(newTodo))                {                    todos.Add(new TodoItem { Title = newTodo });                    newTodo = string.Empty;                }            }        }    }

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>