JavaScript react-bootstrap: clear element value react bootstrap clear form,react-bootstrap clear,react bootstrap typeahead

I'm trying to clear my input fields after an onClick event.

I'm using react-bootstrap library and while there is a getValue() method, there is not setValue(value) method.

I've stumbled upon this discussion .

I did not fully understand what they are suggesting in order to simply clean a form after submission.

After all, If I would use a simple HTML <input> instead of react-bootstrap I could grab the node via element ref and set it's value to be empty string or something.

What is considered a react way to clean my react-bootstrap <Input /> element?

Answer:1

Store the state in your React component, set the element value via props, get the element value via event callbacks. Here is an example:

Here is an example taken directly from their documentation. I just added a clearInput() method to show you you can clear the input by just updating the state of your component. This will trigger a re-render which will cause the input value to update.

const ExampleInput = React.createClass({
  getInitialState() {
    return {
      value: ''
    };
  },

  validationState() {
    let length = this.state.value.length;
    if (length > 10) return 'success';
    else if (length > 5) return 'warning';
    else if (length > 0) return 'error';
  },

  handleChange() {
    // This could also be done using ReactLink:
    // http://facebook.github.io/react/docs/two-way-binding-helpers.html
    this.setState({
      value: this.refs.input.getValue()
    });
  },

  // Example of how you can clear the input by just updating your state
  clearInput() {
    this.setState({ value: "" });
  },

  render() {
    return (
      <Input
        type="text"
        value={this.state.value}
        placeholder="Enter text"
        label="Working example with validation"
        help="Validation is based on string length."
        bsStyle={this.validationState()}
        hasFeedback
        ref="input"
        groupClassName="group-class"
        labelClassName="label-class"
        onChange={this.handleChange} />
    );
  }
});
Answer:2

For what I'm doing at the moment, I didn't really think it was necessary to control the Input component's value through setState/Flux (aka I didn't want to deal with all the boilerplate)...so here's a gist of what I did. Hopefully the React gods forgive me.

import React from 'react';
import { Button, Input } from 'react-bootstrap';

export class BootstrapForm extends React.Component {

  constructor() {
    super();
    this.clearForm = this.clearForm.bind(this);
    this.handleSave = this.handleSave.bind(this);
  }

  clearForm() {
    const fields = ['firstName', 'lastName', 'email'];
    fields.map(field => {
      this.refs[field].refs['input'].value = '';
    });
  }

  handleSubmit() {
    // Handle submitting the form
  }

  render() {
    return (
      <div>
        <form>
          <div>
            <Input
              ref='firstName'
              type='text'
              label='First Name'
              placeholder='Enter First Name'
            />
            <Input
              ref='lastName'
              type='text'
              label='Last Name'
              placeholder='Enter Last Name'
            />
            <Input
              ref='email'
              type='email'
              label='E-Mail'
              placeholder='Enter Email Address'
            /> 
            <div>
              <Button bsStyle={'success'} onClick={this.handleSubmit}>Submit</Button>
              <Button onClick={this.clearForm}>Clear Form</Button>
            </div>
          </div>
        </form>
      </div>
    );
  }
}
Answer:3

You can also just use ReactDOM:

<FormControl
  componentClass="select"
  ref="sStrike">
  <option value="-">Select&hellip;</option>
  <option value="1">1</option>
  <option value="2">2</option>
</FormControl>

Now a different FormControl fires an onChange={handleChange} and in that handler you can just id the ref and set to the default value:

ReactDOM.findDOMNode(this.refs.sStrike).value = '-';

and that will set the select box to the 'default' value.

Answer:4

I am trying to change background color of an anchor tag. And to find this anchor tag I have a span with its id. Then how can I find this anchor tag. Html code is below <li class="fa-2x ...

I am trying to change background color of an anchor tag. And to find this anchor tag I have a span with its id. Then how can I find this anchor tag. Html code is below <li class="fa-2x ...

  1. find closest parent jquery
  2. find closest parent with class jquery
  3. find closest parent with class javascript
  4. find closest parent
  5. find closest parent javascript
  6. find closest parent div jquery
  7. find closest parent with class
  8. find closest parent element jquery
  9. find closest parent js
  10. find closest parent div
  11. find closest parent with class js
  12. find closest parent element
  13. find closest parent tr
  14. find closest parent html
  15. jquery find closest parent element with class
  16. jquery find closest parent div with class
  17. jquery find closest parent with attribute
  18. jquery find closest parent with id
  19. javascript find closest parent element
  20. xpath find closest parent

In my company want to modify one ASp.net Project, <% if(Request.QueryString["cert"]!=null) { %> <asp:TemplateField HeaderText="H&A" ItemStyle-Width="70px"> ...

In my company want to modify one ASp.net Project, <% if(Request.QueryString["cert"]!=null) { %> <asp:TemplateField HeaderText="H&A" ItemStyle-Width="70px"> ...

I am unable to find/pinpoint the root cause of the error. In my javascript file I keep getting a green squiggly under the the word angular. I don't understand why. (Im using Visual Studio Code) ...

I am unable to find/pinpoint the root cause of the error. In my javascript file I keep getting a green squiggly under the the word angular. I don't understand why. (Im using Visual Studio Code) ...

  1. angular error injector modulerr
  2. angular error injector unpr
  3. angular error injector nomod
  4. angular error injector
  5. angular injector error unknown provider
  6. angular uncaught error injector modulerr
  7. angular static injector error
  8. angular null injector error
  9. angular static injector error no provider
  10. angular static injector error httpclient
  11. angular static injector error appmodule
  12. angular debug injector error
  13. angular static injector error no provider for http
  14. angular static injector error http
  15. angularjs injector error
  16. angular service injector error
  17. angular filter injector error
  18. angular module injector error
  19. angular static injector error no provider for t
  20. angular material injector error

Sorry for silly question but I am new to AngularJS. I have a service which will fetch data to HTML page (login.html) in AngularJS. I have login.html page which will have this data when loading. In ...

Sorry for silly question but I am new to AngularJS. I have a service which will fetch data to HTML page (login.html) in AngularJS. I have login.html page which will have this data when loading. In ...