React multi select not able to select negative value












0















Hi i am using react multi select with negative and positive values.When i select -1
it is automatically changed to 1. So not able to select -1. other values working fine.



 class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: '1'};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

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

handleSubmit(event) {
alert('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value={this.state.value} onChange={this.handleChange} multiple>
<option value="-1">Grapefruit</option>
<option value="0">Lime</option>
<option value="1">Coconut</option>
<option value="2">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

ReactDOM.render(
<FlavorForm />,
document.getElementById('root')
);


Please help how to select -1 with react multi select.



Demo










share|improve this question





























    0















    Hi i am using react multi select with negative and positive values.When i select -1
    it is automatically changed to 1. So not able to select -1. other values working fine.



     class FlavorForm extends React.Component {
    constructor(props) {
    super(props);
    this.state = {value: '1'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    }

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

    handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
    }

    render() {
    return (
    <form onSubmit={this.handleSubmit}>
    <label>
    Pick your favorite flavor:
    <select value={this.state.value} onChange={this.handleChange} multiple>
    <option value="-1">Grapefruit</option>
    <option value="0">Lime</option>
    <option value="1">Coconut</option>
    <option value="2">Mango</option>
    </select>
    </label>
    <input type="submit" value="Submit" />
    </form>
    );
    }
    }

    ReactDOM.render(
    <FlavorForm />,
    document.getElementById('root')
    );


    Please help how to select -1 with react multi select.



    Demo










    share|improve this question



























      0












      0








      0








      Hi i am using react multi select with negative and positive values.When i select -1
      it is automatically changed to 1. So not able to select -1. other values working fine.



       class FlavorForm extends React.Component {
      constructor(props) {
      super(props);
      this.state = {value: '1'};

      this.handleChange = this.handleChange.bind(this);
      this.handleSubmit = this.handleSubmit.bind(this);
      }

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

      handleSubmit(event) {
      alert('Your favorite flavor is: ' + this.state.value);
      event.preventDefault();
      }

      render() {
      return (
      <form onSubmit={this.handleSubmit}>
      <label>
      Pick your favorite flavor:
      <select value={this.state.value} onChange={this.handleChange} multiple>
      <option value="-1">Grapefruit</option>
      <option value="0">Lime</option>
      <option value="1">Coconut</option>
      <option value="2">Mango</option>
      </select>
      </label>
      <input type="submit" value="Submit" />
      </form>
      );
      }
      }

      ReactDOM.render(
      <FlavorForm />,
      document.getElementById('root')
      );


      Please help how to select -1 with react multi select.



      Demo










      share|improve this question
















      Hi i am using react multi select with negative and positive values.When i select -1
      it is automatically changed to 1. So not able to select -1. other values working fine.



       class FlavorForm extends React.Component {
      constructor(props) {
      super(props);
      this.state = {value: '1'};

      this.handleChange = this.handleChange.bind(this);
      this.handleSubmit = this.handleSubmit.bind(this);
      }

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

      handleSubmit(event) {
      alert('Your favorite flavor is: ' + this.state.value);
      event.preventDefault();
      }

      render() {
      return (
      <form onSubmit={this.handleSubmit}>
      <label>
      Pick your favorite flavor:
      <select value={this.state.value} onChange={this.handleChange} multiple>
      <option value="-1">Grapefruit</option>
      <option value="0">Lime</option>
      <option value="1">Coconut</option>
      <option value="2">Mango</option>
      </select>
      </label>
      <input type="submit" value="Submit" />
      </form>
      );
      }
      }

      ReactDOM.render(
      <FlavorForm />,
      document.getElementById('root')
      );


      Please help how to select -1 with react multi select.



      Demo







      javascript reactjs multi-select






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 11:46







      ManirajSS

















      asked Nov 21 '18 at 11:38









      ManirajSSManirajSS

      1,27541641




      1,27541641
























          4 Answers
          4






          active

          oldest

          votes


















          0














          You need to store the values property in state as an array, i.e.:



          this.state = { 
          values:
          }


          And then, change handleChange to this:



          handleChange(event) {
          let choices = event.target.selectedOptions;
          let final = ;
          for(let i=0; i<choices.length; i++) {
          final.push(choices[i].value);
          }
          this.setState({ value: final });
          }


          And it should work.



          Here is a link to a working version:
          https://codesandbox.io/s/p3705q0zm



          Cheers!






          share|improve this answer































            1














            Use array for value if multiple is true based on the documentation.
            For multiselect behavior use options as described in this post.






            class FlavorForm extends React.Component {
            constructor(props) {
            super(props);
            this.state = {value: ['1']};

            this.handleChange = this.handleChange.bind(this);
            this.handleSubmit = this.handleSubmit.bind(this);
            }

            handleChange = ({target}) => {
            this.setState(prevState => ({
            value: .filter.call(target.options, o => o.selected).map(o => o.value)
            }));
            }

            handleSubmit(event) {
            alert('Your favorite flavor is: ' + this.state.value);
            event.preventDefault();
            }

            render() {
            return (
            <form onSubmit={this.handleSubmit}>
            <label>
            Pick your favorite flavor:
            <select value={this.state.value} onChange={this.handleChange} multiple>
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
            </select>
            </label>
            <input type="submit" value="Submit" />
            </form>
            );
            }
            }

            ReactDOM.render(
            <FlavorForm />,
            document.getElementById('root')
            );

            <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="root"></div>








            share|improve this answer


























            • But I need multiselect only.Anyhow thanks for your effort!

              – ManirajSS
              Nov 22 '18 at 8:36











            • I upgraded with multiselect behaviort.

              – gazdagergo
              Nov 22 '18 at 18:47



















            0














            I noticed that my console was logging the following error




            Warning: The value prop supplied to must be an array if multiple is true.




            I took the liberty of fixing that and coincidentally the -1 selection started working:



            class FlavorForm extends React.Component {
            constructor (props) {
            super(props)
            this.state = { values: }

            this.handleChange = this.handleChange.bind(this)
            this.handleSubmit = this.handleSubmit.bind(this)
            }

            handleChange (event) {
            const values = this.state.values.includes(event.target.value)
            ? this.state.values
            : this.state.values.concat(event.target.value)
            this.setState({ values: values })
            }

            handleSubmit (event) {
            alert(`Your favorite flavor is: ${this.state.values}`)
            event.preventDefault()
            }

            render () {
            return (
            <form onSubmit={this.handleSubmit}>
            <label>
            Pick your favorite flavor:
            <select
            value={this.state.values}
            onChange={this.handleChange}
            multiple
            >
            <option value="-1">Grapefruit</option>
            <option value="0">Lime</option>
            <option value="1">Coconut</option>
            <option value="2">Mango</option>
            </select>
            </label>
            <input type="submit" value="Submit" />
            </form>
            )
            }
            }





            share|improve this answer































              -1














              Remove value attribute from select element it is invalid there.



              <select onChange={this.handleChange} multiple>...</select>





              share|improve this answer
























              • According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                – Esa Lindqvist
                Nov 21 '18 at 12:03













              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411256%2freact-multi-select-not-able-to-select-negative-value%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You need to store the values property in state as an array, i.e.:



              this.state = { 
              values:
              }


              And then, change handleChange to this:



              handleChange(event) {
              let choices = event.target.selectedOptions;
              let final = ;
              for(let i=0; i<choices.length; i++) {
              final.push(choices[i].value);
              }
              this.setState({ value: final });
              }


              And it should work.



              Here is a link to a working version:
              https://codesandbox.io/s/p3705q0zm



              Cheers!






              share|improve this answer




























                0














                You need to store the values property in state as an array, i.e.:



                this.state = { 
                values:
                }


                And then, change handleChange to this:



                handleChange(event) {
                let choices = event.target.selectedOptions;
                let final = ;
                for(let i=0; i<choices.length; i++) {
                final.push(choices[i].value);
                }
                this.setState({ value: final });
                }


                And it should work.



                Here is a link to a working version:
                https://codesandbox.io/s/p3705q0zm



                Cheers!






                share|improve this answer


























                  0












                  0








                  0







                  You need to store the values property in state as an array, i.e.:



                  this.state = { 
                  values:
                  }


                  And then, change handleChange to this:



                  handleChange(event) {
                  let choices = event.target.selectedOptions;
                  let final = ;
                  for(let i=0; i<choices.length; i++) {
                  final.push(choices[i].value);
                  }
                  this.setState({ value: final });
                  }


                  And it should work.



                  Here is a link to a working version:
                  https://codesandbox.io/s/p3705q0zm



                  Cheers!






                  share|improve this answer













                  You need to store the values property in state as an array, i.e.:



                  this.state = { 
                  values:
                  }


                  And then, change handleChange to this:



                  handleChange(event) {
                  let choices = event.target.selectedOptions;
                  let final = ;
                  for(let i=0; i<choices.length; i++) {
                  final.push(choices[i].value);
                  }
                  this.setState({ value: final });
                  }


                  And it should work.



                  Here is a link to a working version:
                  https://codesandbox.io/s/p3705q0zm



                  Cheers!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 12:03









                  Gabor SzekelyGabor Szekely

                  3079




                  3079

























                      1














                      Use array for value if multiple is true based on the documentation.
                      For multiselect behavior use options as described in this post.






                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>








                      share|improve this answer


























                      • But I need multiselect only.Anyhow thanks for your effort!

                        – ManirajSS
                        Nov 22 '18 at 8:36











                      • I upgraded with multiselect behaviort.

                        – gazdagergo
                        Nov 22 '18 at 18:47
















                      1














                      Use array for value if multiple is true based on the documentation.
                      For multiselect behavior use options as described in this post.






                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>








                      share|improve this answer


























                      • But I need multiselect only.Anyhow thanks for your effort!

                        – ManirajSS
                        Nov 22 '18 at 8:36











                      • I upgraded with multiselect behaviort.

                        – gazdagergo
                        Nov 22 '18 at 18:47














                      1












                      1








                      1







                      Use array for value if multiple is true based on the documentation.
                      For multiselect behavior use options as described in this post.






                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>








                      share|improve this answer















                      Use array for value if multiple is true based on the documentation.
                      For multiselect behavior use options as described in this post.






                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>








                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>





                      class FlavorForm extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {value: ['1']};

                      this.handleChange = this.handleChange.bind(this);
                      this.handleSubmit = this.handleSubmit.bind(this);
                      }

                      handleChange = ({target}) => {
                      this.setState(prevState => ({
                      value: .filter.call(target.options, o => o.selected).map(o => o.value)
                      }));
                      }

                      handleSubmit(event) {
                      alert('Your favorite flavor is: ' + this.state.value);
                      event.preventDefault();
                      }

                      render() {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select value={this.state.value} onChange={this.handleChange} multiple>
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      );
                      }
                      }

                      ReactDOM.render(
                      <FlavorForm />,
                      document.getElementById('root')
                      );

                      <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="root"></div>






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 '18 at 18:46

























                      answered Nov 21 '18 at 12:03









                      gazdagergogazdagergo

                      1,400718




                      1,400718













                      • But I need multiselect only.Anyhow thanks for your effort!

                        – ManirajSS
                        Nov 22 '18 at 8:36











                      • I upgraded with multiselect behaviort.

                        – gazdagergo
                        Nov 22 '18 at 18:47



















                      • But I need multiselect only.Anyhow thanks for your effort!

                        – ManirajSS
                        Nov 22 '18 at 8:36











                      • I upgraded with multiselect behaviort.

                        – gazdagergo
                        Nov 22 '18 at 18:47

















                      But I need multiselect only.Anyhow thanks for your effort!

                      – ManirajSS
                      Nov 22 '18 at 8:36





                      But I need multiselect only.Anyhow thanks for your effort!

                      – ManirajSS
                      Nov 22 '18 at 8:36













                      I upgraded with multiselect behaviort.

                      – gazdagergo
                      Nov 22 '18 at 18:47





                      I upgraded with multiselect behaviort.

                      – gazdagergo
                      Nov 22 '18 at 18:47











                      0














                      I noticed that my console was logging the following error




                      Warning: The value prop supplied to must be an array if multiple is true.




                      I took the liberty of fixing that and coincidentally the -1 selection started working:



                      class FlavorForm extends React.Component {
                      constructor (props) {
                      super(props)
                      this.state = { values: }

                      this.handleChange = this.handleChange.bind(this)
                      this.handleSubmit = this.handleSubmit.bind(this)
                      }

                      handleChange (event) {
                      const values = this.state.values.includes(event.target.value)
                      ? this.state.values
                      : this.state.values.concat(event.target.value)
                      this.setState({ values: values })
                      }

                      handleSubmit (event) {
                      alert(`Your favorite flavor is: ${this.state.values}`)
                      event.preventDefault()
                      }

                      render () {
                      return (
                      <form onSubmit={this.handleSubmit}>
                      <label>
                      Pick your favorite flavor:
                      <select
                      value={this.state.values}
                      onChange={this.handleChange}
                      multiple
                      >
                      <option value="-1">Grapefruit</option>
                      <option value="0">Lime</option>
                      <option value="1">Coconut</option>
                      <option value="2">Mango</option>
                      </select>
                      </label>
                      <input type="submit" value="Submit" />
                      </form>
                      )
                      }
                      }





                      share|improve this answer




























                        0














                        I noticed that my console was logging the following error




                        Warning: The value prop supplied to must be an array if multiple is true.




                        I took the liberty of fixing that and coincidentally the -1 selection started working:



                        class FlavorForm extends React.Component {
                        constructor (props) {
                        super(props)
                        this.state = { values: }

                        this.handleChange = this.handleChange.bind(this)
                        this.handleSubmit = this.handleSubmit.bind(this)
                        }

                        handleChange (event) {
                        const values = this.state.values.includes(event.target.value)
                        ? this.state.values
                        : this.state.values.concat(event.target.value)
                        this.setState({ values: values })
                        }

                        handleSubmit (event) {
                        alert(`Your favorite flavor is: ${this.state.values}`)
                        event.preventDefault()
                        }

                        render () {
                        return (
                        <form onSubmit={this.handleSubmit}>
                        <label>
                        Pick your favorite flavor:
                        <select
                        value={this.state.values}
                        onChange={this.handleChange}
                        multiple
                        >
                        <option value="-1">Grapefruit</option>
                        <option value="0">Lime</option>
                        <option value="1">Coconut</option>
                        <option value="2">Mango</option>
                        </select>
                        </label>
                        <input type="submit" value="Submit" />
                        </form>
                        )
                        }
                        }





                        share|improve this answer


























                          0












                          0








                          0







                          I noticed that my console was logging the following error




                          Warning: The value prop supplied to must be an array if multiple is true.




                          I took the liberty of fixing that and coincidentally the -1 selection started working:



                          class FlavorForm extends React.Component {
                          constructor (props) {
                          super(props)
                          this.state = { values: }

                          this.handleChange = this.handleChange.bind(this)
                          this.handleSubmit = this.handleSubmit.bind(this)
                          }

                          handleChange (event) {
                          const values = this.state.values.includes(event.target.value)
                          ? this.state.values
                          : this.state.values.concat(event.target.value)
                          this.setState({ values: values })
                          }

                          handleSubmit (event) {
                          alert(`Your favorite flavor is: ${this.state.values}`)
                          event.preventDefault()
                          }

                          render () {
                          return (
                          <form onSubmit={this.handleSubmit}>
                          <label>
                          Pick your favorite flavor:
                          <select
                          value={this.state.values}
                          onChange={this.handleChange}
                          multiple
                          >
                          <option value="-1">Grapefruit</option>
                          <option value="0">Lime</option>
                          <option value="1">Coconut</option>
                          <option value="2">Mango</option>
                          </select>
                          </label>
                          <input type="submit" value="Submit" />
                          </form>
                          )
                          }
                          }





                          share|improve this answer













                          I noticed that my console was logging the following error




                          Warning: The value prop supplied to must be an array if multiple is true.




                          I took the liberty of fixing that and coincidentally the -1 selection started working:



                          class FlavorForm extends React.Component {
                          constructor (props) {
                          super(props)
                          this.state = { values: }

                          this.handleChange = this.handleChange.bind(this)
                          this.handleSubmit = this.handleSubmit.bind(this)
                          }

                          handleChange (event) {
                          const values = this.state.values.includes(event.target.value)
                          ? this.state.values
                          : this.state.values.concat(event.target.value)
                          this.setState({ values: values })
                          }

                          handleSubmit (event) {
                          alert(`Your favorite flavor is: ${this.state.values}`)
                          event.preventDefault()
                          }

                          render () {
                          return (
                          <form onSubmit={this.handleSubmit}>
                          <label>
                          Pick your favorite flavor:
                          <select
                          value={this.state.values}
                          onChange={this.handleChange}
                          multiple
                          >
                          <option value="-1">Grapefruit</option>
                          <option value="0">Lime</option>
                          <option value="1">Coconut</option>
                          <option value="2">Mango</option>
                          </select>
                          </label>
                          <input type="submit" value="Submit" />
                          </form>
                          )
                          }
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 11:54









                          Esa LindqvistEsa Lindqvist

                          12116




                          12116























                              -1














                              Remove value attribute from select element it is invalid there.



                              <select onChange={this.handleChange} multiple>...</select>





                              share|improve this answer
























                              • According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                                – Esa Lindqvist
                                Nov 21 '18 at 12:03


















                              -1














                              Remove value attribute from select element it is invalid there.



                              <select onChange={this.handleChange} multiple>...</select>





                              share|improve this answer
























                              • According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                                – Esa Lindqvist
                                Nov 21 '18 at 12:03
















                              -1












                              -1








                              -1







                              Remove value attribute from select element it is invalid there.



                              <select onChange={this.handleChange} multiple>...</select>





                              share|improve this answer













                              Remove value attribute from select element it is invalid there.



                              <select onChange={this.handleChange} multiple>...</select>






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 21 '18 at 11:58









                              alowsarwaralowsarwar

                              328317




                              328317













                              • According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                                – Esa Lindqvist
                                Nov 21 '18 at 12:03





















                              • According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                                – Esa Lindqvist
                                Nov 21 '18 at 12:03



















                              According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                              – Esa Lindqvist
                              Nov 21 '18 at 12:03







                              According to this stackoverflow.com/questions/21733847/… and this reactjs.org/docs/forms.html#the-select-tag using value attribute on the <select> element is the preferred way.

                              – Esa Lindqvist
                              Nov 21 '18 at 12:03




















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411256%2freact-multi-select-not-able-to-select-negative-value%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()