124

I want to pass TextField values when user press enter key from keyboard. In onChange() event, I am getting the value of the textbox, but How to get this value when enter key is pressed ?

Code:

import TextField from 'material-ui/TextField';

class CartridgeShell extends Component {

   constructor(props) {
      super(props);
      this.state = {value:''}
      this.handleChange = this.handleChange.bind(this);
   }

   handleChange(e) {
      this.setState({ value: e.target.value });
   }

   render(){
      return(
         <TextField 
             hintText="First Name" 
             floatingLabelText="First Name*"
             value={this.state.value} 
             onChange={this.handleChange} 
             fullWidth={true} />
      )
   }
}
NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Soumya Behera
  • 2,325
  • 4
  • 15
  • 25
  • 1
    where is your codes to show you tried? – repzero Apr 13 '17 at 05:17
  • It seems like mui prevents use of native browser functionality, forcing manual reimplementation using somewhat lower level code (manually checking keypresses). Am I missing something? (Honest question, I'm new to mui and trying to reorient my thinking.) – ballenf Mar 13 '19 at 17:53

5 Answers5

142

Use onKeyDown event, and inside that check the key code of the key pressed by user. Key code of Enter key is 13, check the code and put the logic there.

Check this example:

class CartridgeShell extends React.Component {

   constructor(props) {
      super(props);
      this.state = {value:''}

      this.handleChange = this.handleChange.bind(this);
      this.keyPress = this.keyPress.bind(this);
   } 
 
   handleChange(e) {
      this.setState({ value: e.target.value });
   }

   keyPress(e){
      if(e.keyCode == 13){
         console.log('value', e.target.value);
         // put the login here
      }
   }

   render(){
      return(
         <input value={this.state.value} onKeyDown={this.keyPress} onChange={this.handleChange} fullWidth={true} />
      )
    }
}

ReactDOM.render(<CartridgeShell/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>


<div id = 'app' />

Note: Replace the input element by Material-Ui TextField and define the other properties also.

Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
  • 7
    [KeyboardEvent.keyCode](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) is deprecated, maybe we should use [KeyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) or [KeyboardEvent.code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) instead ? – Tina Chen Jul 17 '18 at 01:27
  • 13
    Use e.key === "Enter" instead – Gabriel Linassi Oct 09 '20 at 00:35
78

Adding onKeyPress will work onChange in Text Field.

<TextField
  onKeyDown={(ev) => {
    console.log(`Pressed keyCode ${ev.key}`);
    if (ev.key === 'Enter') {
      // Do code here
      ev.preventDefault();
    }
  }}
/>
Liran H
  • 9,143
  • 7
  • 39
  • 52
sunaina kotekar
  • 933
  • 7
  • 7
12

You can use e.target.value to get the current value of the input element if you're using uncontrolled mode.

<TextField
  onKeyPress={(e) => {
    if (e.key === 'Enter') {
      alert(e.target.value);
    }
  }}
/>

Live Demo

Codesandbox Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
2
<input onKeyPress={onKeyPress}/> 

const onKeyPress = (e: any) => { if (e.which == 13) { // your function }};
Skender Ademi
  • 71
  • 1
  • 2
0

html

<input id="something" onkeyup="key_up(this)" type="text">

script

function key_up(e){
    var enterKey = 13; //Key Code for Enter Key
    if (e.which == enterKey){
        //Do you work here
    }
}

Next time, Please try providing some code.

manish
  • 1,450
  • 8
  • 13
  • 1
    Please ask the OP for what they have tried before posting your own code. – Dhiraj Apr 13 '17 at 05:22
  • 1
    why define `enterKey` and using memory when you can just write 13 in the `if` – amir hosein ahmadi Apr 15 '19 at 13:27
  • 10
    @amirhoseinahmadi Nobody cares about memory these days as much as writing clean and understandable code. I have prettier in place in my project which does not let me use any magic numbers. It enforces to use a `const` instead, to define what number is about. I would prefer to see `enterKey` than just `13` – Hafiz Temuri Nov 28 '19 at 15:10
  • 1
    Next time, Please try providing some information about your code. – Patricio Vargas Jan 14 '21 at 06:29