Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear input text after click event

The issue I am having is after entering text for the first time, I then click Add and the input text box gets cleared. However when I start to enter text in again, the input text will not let me enter any text apart from the first letter. I'm not sure why it's doing this.

var FormTextBox = React.createClass({
    handleOnBlur: function (e) {
        this.props.onBlur(e.target.value);
    },
    render: function () {
        return (
            <input value={this.props.value} key={this.props.fid} type="text" onBlur={this.handleOnBlur} />
        )
    }
});

var TestFormTextBox = React.createClass({
    getInitialState: function (e) {
        return {
            value: ''
        }
    },
    handleOnAdd: function (e) {
        this.setState({ value: '' });
    },
    handleTextInfo: function (value) {
        this.setState({ value: value });
    },
    render: function () {
        return (
                <div>
                    <table>
                        <tbody>
                            <tr>
                                <td>Details</td>
                                <td></td>
                            </tr>
                            <tr>
                                <td><FormTextBox value={this.state.value} fid={1} onBlur={this.handleTextInfo} /></td>
                                <td><button onClick={this.handleOnAdd}>Add</button></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
        )
    }
});
like image 396
fes Avatar asked Jul 21 '16 03:07

fes


People also ask

How to clear value of text field on click?

JavaScript clear text field on click. Use Placeholder. Placeholder is a different attribute than value to input fields type (text, password, email, phone) and... Inline JavaScript onClick clear text field value. Inline JavaScript to clear value of text field on click is really... Recommended: Clear ...

How do I clear the input field in a form?

If you are using [ (ngModel)] directive to control your form input fields, then you can clear it by setting an empty string ( ' ') to the ngModel control property. We can also clear it by adding a reference to the input field and access that element inside the app component by using @ViewChild () decorator.

How to clear input on focus in HTML?

Method 1: Clearing Input on Focus. Create an input field. Method 2: Clearing Input with the help of button. Create a button. Get the id of input field. HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

How to clear the input field of an app component?

We can also clear it by adding a reference to the input field and access that element inside the app component by using @ViewChild () decorator. #name is the reference added to the above input field.


2 Answers

I am surprised that this even works the first time. With controlled components in react (ones where you are setting the value like you are with your input). you need to update the value whenever the user changes the text (with the onChange() event).

I made a JS fiddle here with your original code and you can see you can't even update the value in the input. In order to get it to update you need to replace the onBlur event with an onChange event like this JS fiddle. Hope that helps!

like image 180
Dr. Nitpick Avatar answered Oct 25 '22 07:10

Dr. Nitpick


As mentioned here (https://facebook.github.io/react/docs/forms.html#controlled-components),

A controlled has a value prop. Rendering a controlled will reflect the value of the value prop.

User input will have no effect on the rendered element because React has declared the value to be Hello!. To update the value in response to user input, you could use the onChange event

You need to either change onBlur to onChange, or use defaultValue instead of value. e.g.

<input defaultValue={this.props.value} key={this.props.fid} type="text" onBlur={this.handleOnBlur} />
like image 39
mnsr Avatar answered Oct 25 '22 05:10

mnsr