0

I have an input tag of type="text" that I don't want to have a submit button for. I want it to be "submitted" for when the user presses ENTER, and when they press enter it should redirect them to a different page that I already have set up to reroute to.

I don't know how to "submit" an input tag on "enter" and then reroute the page on "enter." Any ideas?

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Surya
  • 576
  • 1
  • 4
  • 16
  • Just put it in a `
    `. Rerouting is a different problem you need to look up separately, and please check for existing answers before posting another question.
    –  May 20 '20 at 06:44
  • Most browsers will let you submit the form of single input field on hitting ENTER in field. But it is best to keep a `` button. If you don't want to show it, hide it with CSS. – Ajeet Shah May 20 '20 at 06:48
  • ```Enter``` or ```Return``` key ascii code is 13 if i remember well, So i believe it is possible to use it in ```onKeyPress``` prop. Need test. – Vahid Alimohamadi May 20 '20 at 06:50

1 Answers1

0

You can find out which key pressed, I tested it in codeSandBox and no Ascii code check need, like this:


      <input type="text"
        onKeyPress={e=>e.key==='Enter' && alert ('enter pressed')}
      />

Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37