0

I am trying to get the Enter key to submit this form but it is refreshing the page instead. Any Solutions?

<form onSubmit={() => handleSubmit()}> <label for="text">Enter Task</label> <input type="text" id="name" value = {input} onInput = {(e) => setInput(e.target.value)}></input> <button type = "submit">Add</button> </form> 

This is my handleSubmit() function:

const handleSubmit = e => { e.preventDefault() //function logic 

I have tried using the preventDefault() function but that does not seem to work

2
  • Instead of onSubmit use onClick. Commented Dec 23, 2022 at 12:27
  • I remember doing this by adding an eventListener to the holding div for when the enter key is pressed, then run the submit function on capture. Commented Dec 23, 2022 at 13:23

1 Answer 1

1

I think the issue you have here is that e.preventDefault() is not working as expected because e is not defined. You need to pass params to handleSubmit() doing one of the following

<form onSubmit={handleSubmit}></form> <form onSubmit={(e) => handleSubmit(e)}></form> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.