0
const emailInput = document.getElementById("loginEmail");
emailInput.value = ''
...
<input
  id="loginEmail"
  value={state.email}
  type="input" 
  className="input-text" 
  name="email" 
  placeholder={txt.emailAddress}
  onChange={formValue}
></input>

The error I'm getting on line 2 in the example above is `Property 'value' does not exist on type 'HTMLElement'.ts(2339)

`

2 Answers 2

2

This is how you should control the value of input in React

<input
  id="loginEmail"
  value={this.state.email}  
  type="input" 
  className="input-text" 
  name="email" 
  placeholder={txt.emailAddress}
  onChange={formValue}
></input>

In order to clear a value:

this.setState({
  email: ''
});
Sign up to request clarification or add additional context in comments.

Comments

0

You have to assign the value for input element. To clear the value just reset the state variable.

this.setState({ email: "" });

Here is the small code for you

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      inputVal: "Clue Mediator"
    };
    this.onInputChange = this.onInputChange.bind(this);
    this.clearInput = this.clearInput.bind(this);
  }

  clearInput() {
    this.setState({ inputVal: "" });
  }

  // handle input change event
  onInputChange(e) {
    this.setState({ inputVal: e.target.value });
  }

  render() {
    return (
      <>
        <input
          placeholder="Enter your value here..."
          value={this.state.inputVal}
          onChange={this.onInputChange}
        />
        <input type="button" value="clear" onClick={this.clearInput} />
      </>
    );
  }
}

Here is the working demo for you
https://codesandbox.io/s/autumn-darkness-rybj2

Hope this will work for you!

3 Comments

the value is whatever the user types, so how do i assign it?
If you are work with reactjs then create state variable and assign in value. Check my updated answer for it.
Here is the working demo for you codesandbox.io/s/autumn-darkness-rybj2. I have updated in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.