Retrieving value from with multiple option in React Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How handle multiple select form in ReactJSreact-select multiple optionReact multi select not able to select negative valueHow do I make react handler working for multiple values?How do you remove all the options of a select box and then add one option and select it with jQuery?What is the best way to add options to a select from as a JS object with jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryjQuery Get Selected Option From DropdownReact JSX: selecting “selected” on selected <select> optionGet selected option text using react js?setState with <select> options with values as objectsReact using react-select with multiple <Select /> tags

Table formatting with tabularx?

Which types of prepositional phrase is "toward its employees" in Philosophy guiding the organization's policies towards its employees is not bad?

What does 丫 mean? 丫是什么意思?

How to ask rejected full-time candidates to apply to teach individual courses?

How to make triangles with rounded sides and corners? (squircle with 3 sides)

Can I cut the hair of a conjured korred with a blade made of precious material to harvest that material from the korred?

An isoperimetric-type inequality inside a cube

Why do the Z-fighters hide their power?

draw a pulley system

New Order #6: Easter Egg

Twin's vs. Twins'

Improvising over quartal voicings

why doesn't university give past final exams' answers

Random body shuffle every night—can we still function?

Short story about astronauts fertilizing soil with their own bodies

Plotting a Maclaurin series

Does the universe have a fixed centre of mass?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

What is "Lambda" in Heston's original paper on stochastic volatility models?

Fit odd number of triplets in a measure?

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

Is there night in Alpha Complex?

Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?

Keep at all times, the minus sign above aligned with minus sign below



Retrieving value from with multiple option in React



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How handle multiple select form in ReactJSreact-select multiple optionReact multi select not able to select negative valueHow do I make react handler working for multiple values?How do you remove all the options of a select box and then add one option and select it with jQuery?What is the best way to add options to a select from as a JS object with jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?Get selected text from a drop-down list (select box) using jQueryjQuery Get Selected Option From DropdownReact JSX: selecting “selected” on selected <select> optionGet selected option text using react js?setState with <select> options with values as objectsReact using react-select with multiple <Select /> tags



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








51















The React way to set which option is selected for a select box, is to set a special value prop on the <select> itself, corresponding to the value attribute on the <option> element you desire to be selected. For a multiple select this prop can accept an array instead. (Edit: Currently the documentation seems to have removed reference to this)



Now because this is a special attribute, I'm wondering what the canonical way is to retrieve the selected options in the same array-of-option-values-structure when the user changes things (so I can pass it through a callback to a parent component etc), since presumably the same value property won't be available on the DOM element.



To use an example, with a text field you would do something like this (JSX):



var TextComponent = React.createClass(
handleChange: function(e)
var newText = e.target.value;
this.props.someCallbackFromParent(newText);
,
render: function()
return <input type="text" value=this.props.someText onChange=this.handleChange />;

);


What is the equivalent to replace ??? for this multiple select component?



var MultiSelectComponent = React.createClass(
handleChange: function(e)
var newArrayOfSelectedOptionValues = ???;
this.props.someCallbackFromParent(newArrayOfSelectedOptionValues);
,
render: function()
return (
<select multiple=true value=this.props.arrayOfOptionValues onChange=this.handleChange>
<option value=1>First option</option>
<option value=2>Second option</option>
<option value=3>Third option</option>
</select>
);

);









share|improve this question






























    51















    The React way to set which option is selected for a select box, is to set a special value prop on the <select> itself, corresponding to the value attribute on the <option> element you desire to be selected. For a multiple select this prop can accept an array instead. (Edit: Currently the documentation seems to have removed reference to this)



    Now because this is a special attribute, I'm wondering what the canonical way is to retrieve the selected options in the same array-of-option-values-structure when the user changes things (so I can pass it through a callback to a parent component etc), since presumably the same value property won't be available on the DOM element.



    To use an example, with a text field you would do something like this (JSX):



    var TextComponent = React.createClass(
    handleChange: function(e)
    var newText = e.target.value;
    this.props.someCallbackFromParent(newText);
    ,
    render: function()
    return <input type="text" value=this.props.someText onChange=this.handleChange />;

    );


    What is the equivalent to replace ??? for this multiple select component?



    var MultiSelectComponent = React.createClass(
    handleChange: function(e)
    var newArrayOfSelectedOptionValues = ???;
    this.props.someCallbackFromParent(newArrayOfSelectedOptionValues);
    ,
    render: function()
    return (
    <select multiple=true value=this.props.arrayOfOptionValues onChange=this.handleChange>
    <option value=1>First option</option>
    <option value=2>Second option</option>
    <option value=3>Third option</option>
    </select>
    );

    );









    share|improve this question


























      51












      51








      51


      15






      The React way to set which option is selected for a select box, is to set a special value prop on the <select> itself, corresponding to the value attribute on the <option> element you desire to be selected. For a multiple select this prop can accept an array instead. (Edit: Currently the documentation seems to have removed reference to this)



      Now because this is a special attribute, I'm wondering what the canonical way is to retrieve the selected options in the same array-of-option-values-structure when the user changes things (so I can pass it through a callback to a parent component etc), since presumably the same value property won't be available on the DOM element.



      To use an example, with a text field you would do something like this (JSX):



      var TextComponent = React.createClass(
      handleChange: function(e)
      var newText = e.target.value;
      this.props.someCallbackFromParent(newText);
      ,
      render: function()
      return <input type="text" value=this.props.someText onChange=this.handleChange />;

      );


      What is the equivalent to replace ??? for this multiple select component?



      var MultiSelectComponent = React.createClass(
      handleChange: function(e)
      var newArrayOfSelectedOptionValues = ???;
      this.props.someCallbackFromParent(newArrayOfSelectedOptionValues);
      ,
      render: function()
      return (
      <select multiple=true value=this.props.arrayOfOptionValues onChange=this.handleChange>
      <option value=1>First option</option>
      <option value=2>Second option</option>
      <option value=3>Third option</option>
      </select>
      );

      );









      share|improve this question
















      The React way to set which option is selected for a select box, is to set a special value prop on the <select> itself, corresponding to the value attribute on the <option> element you desire to be selected. For a multiple select this prop can accept an array instead. (Edit: Currently the documentation seems to have removed reference to this)



      Now because this is a special attribute, I'm wondering what the canonical way is to retrieve the selected options in the same array-of-option-values-structure when the user changes things (so I can pass it through a callback to a parent component etc), since presumably the same value property won't be available on the DOM element.



      To use an example, with a text field you would do something like this (JSX):



      var TextComponent = React.createClass(
      handleChange: function(e)
      var newText = e.target.value;
      this.props.someCallbackFromParent(newText);
      ,
      render: function()
      return <input type="text" value=this.props.someText onChange=this.handleChange />;

      );


      What is the equivalent to replace ??? for this multiple select component?



      var MultiSelectComponent = React.createClass(
      handleChange: function(e)
      var newArrayOfSelectedOptionValues = ???;
      this.props.someCallbackFromParent(newArrayOfSelectedOptionValues);
      ,
      render: function()
      return (
      <select multiple=true value=this.props.arrayOfOptionValues onChange=this.handleChange>
      <option value=1>First option</option>
      <option value=2>Second option</option>
      <option value=3>Third option</option>
      </select>
      );

      );






      javascript dom reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 1:11







      Inkling

















      asked Feb 20 '15 at 8:43









      InklingInkling

      1,49122028




      1,49122028






















          9 Answers
          9






          active

          oldest

          votes


















          76














          The same way you do anywhere else, since you're working with the real DOM node as the target of the change event:



          handleChange: function(e) 
          var options = e.target.options;
          var value = [];
          for (var i = 0, l = options.length; i < l; i++)
          if (options[i].selected)
          value.push(options[i].value);


          this.props.someCallback(value);






          share|improve this answer




















          • 3





            CoffeeScript version: (option.value for option in e.target.options when option.selected)

            – 1j01
            Jul 1 '15 at 13:33






          • 48





            ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

            – svachalek
            Aug 14 '15 at 22:21






          • 1





            destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

            – Charlie Martin
            Jun 21 '16 at 19:12







          • 3





            You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

            – Mathieu M-Gosselin
            Sep 27 '16 at 14:33






          • 2





            RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

            – TrueWill
            Aug 8 '18 at 17:35


















          11














          In case you want to use ref you can get selected values like this:



          var select = React.findDOMNode(this.refs.selectRef); 
          var values = [].filter.call(select.options, function (o)
          return o.selected;
          ).map(function (o)
          return o.value;
          );


          2018 ES6 update



           let select = this.refs.selectRef;
          let values = [].filter.call(select.options, o => o.selected).map(o => o.value);





          share|improve this answer

























          • This worked great for me, thank you.

            – pyRabbit
            Feb 8 '16 at 23:00






          • 1





            will use this to substitute for my answer as well, since selectedOptions is not supported in IE

            – rambossa
            Apr 6 '16 at 11:20












          • Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

            – Dg Jacquard
            Apr 2 '18 at 21:55



















          7














          In the case you would like to keep track of the selected options while the form is being completed:



          handleChange(e) 
          // assuming you initialized the default state to hold selected values
          this.setState(
          selected:[].slice.call(e.target.selectedOptions).map(o =>
          return o.value;
          );
          );



          selectedOptions is an array-like collection/list of elements related to the DOM. You get access to it in the event target object when selecting option values. Array.prototype.sliceand call allows us to create a copy of it for the new state. You could also access the values this way using a ref (in case you aren't capturing the event), which was another answer for the question.






          share|improve this answer




















          • 2





            As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

            – Inkling
            Apr 6 '16 at 3:16







          • 1





            Ah true, it seems like IE still does not support this

            – rambossa
            Apr 6 '16 at 11:16


















          6














          Easiest way...



          handleChange(evt) 
          this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));






          share|improve this answer






























            3














            The following worked for me



            var selectBoxObj = React.findDOMNode(this.refs.selectBox)
            var values = $(selectBoxObj).val()





            share|improve this answer























            • Using jQuery, correct?

              – Inkling
              Aug 20 '15 at 0:50






            • 1





              Correct, I used JQuery to retrieve the multiple values.

              – mantish
              Aug 20 '15 at 7:13











            • You don't need react dom or jQuery. Just retrieve the value from the onChange event

              – jamesmfriedman
              Dec 19 '17 at 20:27


















            2














            With Array.from() and e.target.selectedOptions you can perform a controlled select-multiple:



            handleChange = (e) => 
            let value = Array.from(e.target.selectedOptions, option => option.value);
            this.setState(values: value);



            target.selectedOptions return a HTMLCollection



            https://codepen.io/papawa/pen/XExeZY






            share|improve this answer

























            • Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

              – Casimir
              Apr 29 '18 at 16:17


















            1














            Another way to do it:



            handleChange( target ) 
            this.props.someCallback(
            Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
            )






            share|improve this answer






























              0














              Try this one:



              dropdownChanged = (event) => 
              let obj = JSON.parse(event.target.value);
              this.setState(

              key: obj.key,
              selectedName: obj.name,
              type: obj.type
              );



              <select onChange=this.dropdownChanged >
              <option value=JSON.stringify( name: 'name', key: 'key', type: "ALL" ) >All Projetcs and Spaces</option></select>





              share|improve this answer

























              • Add some more explanation to your solutions.

                – Manmohan_singh
                Mar 26 '18 at 14:10


















              0














              You can actually find the selectedOptions inside the target... no need to iterate over all the options. Let's imagine you want to send the values to an onChange function passed to your component as props: you can use the following function on the onChange of your multiple select.



              onSelectChange = (e) => 
              const values = [...e.target.selectedOptions].map(opt => opt.value);
              this.props.onChange(values);
              ;





              share|improve this answer

























              • At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                – Inkling
                Mar 9 at 1:09











              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%2f28624763%2fretrieving-value-from-select-with-multiple-option-in-react%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              76














              The same way you do anywhere else, since you're working with the real DOM node as the target of the change event:



              handleChange: function(e) 
              var options = e.target.options;
              var value = [];
              for (var i = 0, l = options.length; i < l; i++)
              if (options[i].selected)
              value.push(options[i].value);


              this.props.someCallback(value);






              share|improve this answer




















              • 3





                CoffeeScript version: (option.value for option in e.target.options when option.selected)

                – 1j01
                Jul 1 '15 at 13:33






              • 48





                ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

                – svachalek
                Aug 14 '15 at 22:21






              • 1





                destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

                – Charlie Martin
                Jun 21 '16 at 19:12







              • 3





                You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

                – Mathieu M-Gosselin
                Sep 27 '16 at 14:33






              • 2





                RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

                – TrueWill
                Aug 8 '18 at 17:35















              76














              The same way you do anywhere else, since you're working with the real DOM node as the target of the change event:



              handleChange: function(e) 
              var options = e.target.options;
              var value = [];
              for (var i = 0, l = options.length; i < l; i++)
              if (options[i].selected)
              value.push(options[i].value);


              this.props.someCallback(value);






              share|improve this answer




















              • 3





                CoffeeScript version: (option.value for option in e.target.options when option.selected)

                – 1j01
                Jul 1 '15 at 13:33






              • 48





                ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

                – svachalek
                Aug 14 '15 at 22:21






              • 1





                destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

                – Charlie Martin
                Jun 21 '16 at 19:12







              • 3





                You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

                – Mathieu M-Gosselin
                Sep 27 '16 at 14:33






              • 2





                RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

                – TrueWill
                Aug 8 '18 at 17:35













              76












              76








              76







              The same way you do anywhere else, since you're working with the real DOM node as the target of the change event:



              handleChange: function(e) 
              var options = e.target.options;
              var value = [];
              for (var i = 0, l = options.length; i < l; i++)
              if (options[i].selected)
              value.push(options[i].value);


              this.props.someCallback(value);






              share|improve this answer















              The same way you do anywhere else, since you're working with the real DOM node as the target of the change event:



              handleChange: function(e) 
              var options = e.target.options;
              var value = [];
              for (var i = 0, l = options.length; i < l; i++)
              if (options[i].selected)
              value.push(options[i].value);


              this.props.someCallback(value);







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 25 '15 at 17:00

























              answered Feb 20 '15 at 9:23









              Jonny BuchananJonny Buchanan

              50.6k10127141




              50.6k10127141







              • 3





                CoffeeScript version: (option.value for option in e.target.options when option.selected)

                – 1j01
                Jul 1 '15 at 13:33






              • 48





                ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

                – svachalek
                Aug 14 '15 at 22:21






              • 1





                destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

                – Charlie Martin
                Jun 21 '16 at 19:12







              • 3





                You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

                – Mathieu M-Gosselin
                Sep 27 '16 at 14:33






              • 2





                RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

                – TrueWill
                Aug 8 '18 at 17:35












              • 3





                CoffeeScript version: (option.value for option in e.target.options when option.selected)

                – 1j01
                Jul 1 '15 at 13:33






              • 48





                ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

                – svachalek
                Aug 14 '15 at 22:21






              • 1





                destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

                – Charlie Martin
                Jun 21 '16 at 19:12







              • 3





                You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

                – Mathieu M-Gosselin
                Sep 27 '16 at 14:33






              • 2





                RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

                – TrueWill
                Aug 8 '18 at 17:35







              3




              3





              CoffeeScript version: (option.value for option in e.target.options when option.selected)

              – 1j01
              Jul 1 '15 at 13:33





              CoffeeScript version: (option.value for option in e.target.options when option.selected)

              – 1j01
              Jul 1 '15 at 13:33




              48




              48





              ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

              – svachalek
              Aug 14 '15 at 22:21





              ES6 version: [...event.target.options].filter(o => o.selected).map(o => o.value)

              – svachalek
              Aug 14 '15 at 22:21




              1




              1





              destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

              – Charlie Martin
              Jun 21 '16 at 19:12






              destructure the params to make that es6 version even nicer... handleChange: ( target: options ) => [...options].filter

              – Charlie Martin
              Jun 21 '16 at 19:12





              3




              3





              You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

              – Mathieu M-Gosselin
              Sep 27 '16 at 14:33





              You can also destructure the ES6 arrows: [...e.target.options].filter((selected) => selected).map((value) => value)

              – Mathieu M-Gosselin
              Sep 27 '16 at 14:33




              2




              2





              RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

              – TrueWill
              Aug 8 '18 at 17:35





              RE: ES6 version, while that looks right it doesn't work. event.target.options is an HTMLOptionsCollection, not an Array.

              – TrueWill
              Aug 8 '18 at 17:35













              11














              In case you want to use ref you can get selected values like this:



              var select = React.findDOMNode(this.refs.selectRef); 
              var values = [].filter.call(select.options, function (o)
              return o.selected;
              ).map(function (o)
              return o.value;
              );


              2018 ES6 update



               let select = this.refs.selectRef;
              let values = [].filter.call(select.options, o => o.selected).map(o => o.value);





              share|improve this answer

























              • This worked great for me, thank you.

                – pyRabbit
                Feb 8 '16 at 23:00






              • 1





                will use this to substitute for my answer as well, since selectedOptions is not supported in IE

                – rambossa
                Apr 6 '16 at 11:20












              • Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

                – Dg Jacquard
                Apr 2 '18 at 21:55
















              11














              In case you want to use ref you can get selected values like this:



              var select = React.findDOMNode(this.refs.selectRef); 
              var values = [].filter.call(select.options, function (o)
              return o.selected;
              ).map(function (o)
              return o.value;
              );


              2018 ES6 update



               let select = this.refs.selectRef;
              let values = [].filter.call(select.options, o => o.selected).map(o => o.value);





              share|improve this answer

























              • This worked great for me, thank you.

                – pyRabbit
                Feb 8 '16 at 23:00






              • 1





                will use this to substitute for my answer as well, since selectedOptions is not supported in IE

                – rambossa
                Apr 6 '16 at 11:20












              • Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

                – Dg Jacquard
                Apr 2 '18 at 21:55














              11












              11








              11







              In case you want to use ref you can get selected values like this:



              var select = React.findDOMNode(this.refs.selectRef); 
              var values = [].filter.call(select.options, function (o)
              return o.selected;
              ).map(function (o)
              return o.value;
              );


              2018 ES6 update



               let select = this.refs.selectRef;
              let values = [].filter.call(select.options, o => o.selected).map(o => o.value);





              share|improve this answer















              In case you want to use ref you can get selected values like this:



              var select = React.findDOMNode(this.refs.selectRef); 
              var values = [].filter.call(select.options, function (o)
              return o.selected;
              ).map(function (o)
              return o.value;
              );


              2018 ES6 update



               let select = this.refs.selectRef;
              let values = [].filter.call(select.options, o => o.selected).map(o => o.value);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 6 '18 at 17:18









              Deano

              4,673113975




              4,673113975










              answered Oct 17 '15 at 9:30









              Max PodriezovMax Podriezov

              430610




              430610












              • This worked great for me, thank you.

                – pyRabbit
                Feb 8 '16 at 23:00






              • 1





                will use this to substitute for my answer as well, since selectedOptions is not supported in IE

                – rambossa
                Apr 6 '16 at 11:20












              • Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

                – Dg Jacquard
                Apr 2 '18 at 21:55


















              • This worked great for me, thank you.

                – pyRabbit
                Feb 8 '16 at 23:00






              • 1





                will use this to substitute for my answer as well, since selectedOptions is not supported in IE

                – rambossa
                Apr 6 '16 at 11:20












              • Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

                – Dg Jacquard
                Apr 2 '18 at 21:55

















              This worked great for me, thank you.

              – pyRabbit
              Feb 8 '16 at 23:00





              This worked great for me, thank you.

              – pyRabbit
              Feb 8 '16 at 23:00




              1




              1





              will use this to substitute for my answer as well, since selectedOptions is not supported in IE

              – rambossa
              Apr 6 '16 at 11:20






              will use this to substitute for my answer as well, since selectedOptions is not supported in IE

              – rambossa
              Apr 6 '16 at 11:20














              Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

              – Dg Jacquard
              Apr 2 '18 at 21:55






              Here's a much cleaner, es6 way to do it :) [].filter.call(this.refs.selectRef.options, o => o.selected).map(o => o.value);

              – Dg Jacquard
              Apr 2 '18 at 21:55












              7














              In the case you would like to keep track of the selected options while the form is being completed:



              handleChange(e) 
              // assuming you initialized the default state to hold selected values
              this.setState(
              selected:[].slice.call(e.target.selectedOptions).map(o =>
              return o.value;
              );
              );



              selectedOptions is an array-like collection/list of elements related to the DOM. You get access to it in the event target object when selecting option values. Array.prototype.sliceand call allows us to create a copy of it for the new state. You could also access the values this way using a ref (in case you aren't capturing the event), which was another answer for the question.






              share|improve this answer




















              • 2





                As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

                – Inkling
                Apr 6 '16 at 3:16







              • 1





                Ah true, it seems like IE still does not support this

                – rambossa
                Apr 6 '16 at 11:16















              7














              In the case you would like to keep track of the selected options while the form is being completed:



              handleChange(e) 
              // assuming you initialized the default state to hold selected values
              this.setState(
              selected:[].slice.call(e.target.selectedOptions).map(o =>
              return o.value;
              );
              );



              selectedOptions is an array-like collection/list of elements related to the DOM. You get access to it in the event target object when selecting option values. Array.prototype.sliceand call allows us to create a copy of it for the new state. You could also access the values this way using a ref (in case you aren't capturing the event), which was another answer for the question.






              share|improve this answer




















              • 2





                As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

                – Inkling
                Apr 6 '16 at 3:16







              • 1





                Ah true, it seems like IE still does not support this

                – rambossa
                Apr 6 '16 at 11:16













              7












              7








              7







              In the case you would like to keep track of the selected options while the form is being completed:



              handleChange(e) 
              // assuming you initialized the default state to hold selected values
              this.setState(
              selected:[].slice.call(e.target.selectedOptions).map(o =>
              return o.value;
              );
              );



              selectedOptions is an array-like collection/list of elements related to the DOM. You get access to it in the event target object when selecting option values. Array.prototype.sliceand call allows us to create a copy of it for the new state. You could also access the values this way using a ref (in case you aren't capturing the event), which was another answer for the question.






              share|improve this answer















              In the case you would like to keep track of the selected options while the form is being completed:



              handleChange(e) 
              // assuming you initialized the default state to hold selected values
              this.setState(
              selected:[].slice.call(e.target.selectedOptions).map(o =>
              return o.value;
              );
              );



              selectedOptions is an array-like collection/list of elements related to the DOM. You get access to it in the event target object when selecting option values. Array.prototype.sliceand call allows us to create a copy of it for the new state. You could also access the values this way using a ref (in case you aren't capturing the event), which was another answer for the question.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 5 '16 at 15:54

























              answered Apr 5 '16 at 15:44









              rambossarambossa

              2,78542139




              2,78542139







              • 2





                As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

                – Inkling
                Apr 6 '16 at 3:16







              • 1





                Ah true, it seems like IE still does not support this

                – rambossa
                Apr 6 '16 at 11:16












              • 2





                As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

                – Inkling
                Apr 6 '16 at 3:16







              • 1





                Ah true, it seems like IE still does not support this

                – rambossa
                Apr 6 '16 at 11:16







              2




              2





              As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

              – Inkling
              Apr 6 '16 at 3:16






              As mentioned in one of my other comments, browser support for selectedOptions seems pretty sketchy. But it would probably be the ideal solution if the support was there.

              – Inkling
              Apr 6 '16 at 3:16





              1




              1





              Ah true, it seems like IE still does not support this

              – rambossa
              Apr 6 '16 at 11:16





              Ah true, it seems like IE still does not support this

              – rambossa
              Apr 6 '16 at 11:16











              6














              Easiest way...



              handleChange(evt) 
              this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));






              share|improve this answer



























                6














                Easiest way...



                handleChange(evt) 
                this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));






                share|improve this answer

























                  6












                  6








                  6







                  Easiest way...



                  handleChange(evt) 
                  this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));






                  share|improve this answer













                  Easiest way...



                  handleChange(evt) 
                  this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '17 at 20:26









                  jamesmfriedmanjamesmfriedman

                  42555




                  42555





















                      3














                      The following worked for me



                      var selectBoxObj = React.findDOMNode(this.refs.selectBox)
                      var values = $(selectBoxObj).val()





                      share|improve this answer























                      • Using jQuery, correct?

                        – Inkling
                        Aug 20 '15 at 0:50






                      • 1





                        Correct, I used JQuery to retrieve the multiple values.

                        – mantish
                        Aug 20 '15 at 7:13











                      • You don't need react dom or jQuery. Just retrieve the value from the onChange event

                        – jamesmfriedman
                        Dec 19 '17 at 20:27















                      3














                      The following worked for me



                      var selectBoxObj = React.findDOMNode(this.refs.selectBox)
                      var values = $(selectBoxObj).val()





                      share|improve this answer























                      • Using jQuery, correct?

                        – Inkling
                        Aug 20 '15 at 0:50






                      • 1





                        Correct, I used JQuery to retrieve the multiple values.

                        – mantish
                        Aug 20 '15 at 7:13











                      • You don't need react dom or jQuery. Just retrieve the value from the onChange event

                        – jamesmfriedman
                        Dec 19 '17 at 20:27













                      3












                      3








                      3







                      The following worked for me



                      var selectBoxObj = React.findDOMNode(this.refs.selectBox)
                      var values = $(selectBoxObj).val()





                      share|improve this answer













                      The following worked for me



                      var selectBoxObj = React.findDOMNode(this.refs.selectBox)
                      var values = $(selectBoxObj).val()






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 19 '15 at 14:50









                      mantishmantish

                      34129




                      34129












                      • Using jQuery, correct?

                        – Inkling
                        Aug 20 '15 at 0:50






                      • 1





                        Correct, I used JQuery to retrieve the multiple values.

                        – mantish
                        Aug 20 '15 at 7:13











                      • You don't need react dom or jQuery. Just retrieve the value from the onChange event

                        – jamesmfriedman
                        Dec 19 '17 at 20:27

















                      • Using jQuery, correct?

                        – Inkling
                        Aug 20 '15 at 0:50






                      • 1





                        Correct, I used JQuery to retrieve the multiple values.

                        – mantish
                        Aug 20 '15 at 7:13











                      • You don't need react dom or jQuery. Just retrieve the value from the onChange event

                        – jamesmfriedman
                        Dec 19 '17 at 20:27
















                      Using jQuery, correct?

                      – Inkling
                      Aug 20 '15 at 0:50





                      Using jQuery, correct?

                      – Inkling
                      Aug 20 '15 at 0:50




                      1




                      1





                      Correct, I used JQuery to retrieve the multiple values.

                      – mantish
                      Aug 20 '15 at 7:13





                      Correct, I used JQuery to retrieve the multiple values.

                      – mantish
                      Aug 20 '15 at 7:13













                      You don't need react dom or jQuery. Just retrieve the value from the onChange event

                      – jamesmfriedman
                      Dec 19 '17 at 20:27





                      You don't need react dom or jQuery. Just retrieve the value from the onChange event

                      – jamesmfriedman
                      Dec 19 '17 at 20:27











                      2














                      With Array.from() and e.target.selectedOptions you can perform a controlled select-multiple:



                      handleChange = (e) => 
                      let value = Array.from(e.target.selectedOptions, option => option.value);
                      this.setState(values: value);



                      target.selectedOptions return a HTMLCollection



                      https://codepen.io/papawa/pen/XExeZY






                      share|improve this answer

























                      • Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                        – Casimir
                        Apr 29 '18 at 16:17















                      2














                      With Array.from() and e.target.selectedOptions you can perform a controlled select-multiple:



                      handleChange = (e) => 
                      let value = Array.from(e.target.selectedOptions, option => option.value);
                      this.setState(values: value);



                      target.selectedOptions return a HTMLCollection



                      https://codepen.io/papawa/pen/XExeZY






                      share|improve this answer

























                      • Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                        – Casimir
                        Apr 29 '18 at 16:17













                      2












                      2








                      2







                      With Array.from() and e.target.selectedOptions you can perform a controlled select-multiple:



                      handleChange = (e) => 
                      let value = Array.from(e.target.selectedOptions, option => option.value);
                      this.setState(values: value);



                      target.selectedOptions return a HTMLCollection



                      https://codepen.io/papawa/pen/XExeZY






                      share|improve this answer















                      With Array.from() and e.target.selectedOptions you can perform a controlled select-multiple:



                      handleChange = (e) => 
                      let value = Array.from(e.target.selectedOptions, option => option.value);
                      this.setState(values: value);



                      target.selectedOptions return a HTMLCollection



                      https://codepen.io/papawa/pen/XExeZY







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 6 '18 at 2:09

























                      answered Apr 6 '18 at 2:01









                      MendesMendes

                      506




                      506












                      • Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                        – Casimir
                        Apr 29 '18 at 16:17

















                      • Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                        – Casimir
                        Apr 29 '18 at 16:17
















                      Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                      – Casimir
                      Apr 29 '18 at 16:17





                      Any ideas on how to combine the selected options from multiple selects into one array? Say after grabbing them all via document.querySelectorAll('select')?

                      – Casimir
                      Apr 29 '18 at 16:17











                      1














                      Another way to do it:



                      handleChange( target ) 
                      this.props.someCallback(
                      Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
                      )






                      share|improve this answer



























                        1














                        Another way to do it:



                        handleChange( target ) 
                        this.props.someCallback(
                        Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
                        )






                        share|improve this answer

























                          1












                          1








                          1







                          Another way to do it:



                          handleChange( target ) 
                          this.props.someCallback(
                          Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
                          )






                          share|improve this answer













                          Another way to do it:



                          handleChange( target ) 
                          this.props.someCallback(
                          Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
                          )







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 30 '17 at 8:42









                          Aral RocaAral Roca

                          1,86322447




                          1,86322447





















                              0














                              Try this one:



                              dropdownChanged = (event) => 
                              let obj = JSON.parse(event.target.value);
                              this.setState(

                              key: obj.key,
                              selectedName: obj.name,
                              type: obj.type
                              );



                              <select onChange=this.dropdownChanged >
                              <option value=JSON.stringify( name: 'name', key: 'key', type: "ALL" ) >All Projetcs and Spaces</option></select>





                              share|improve this answer

























                              • Add some more explanation to your solutions.

                                – Manmohan_singh
                                Mar 26 '18 at 14:10















                              0














                              Try this one:



                              dropdownChanged = (event) => 
                              let obj = JSON.parse(event.target.value);
                              this.setState(

                              key: obj.key,
                              selectedName: obj.name,
                              type: obj.type
                              );



                              <select onChange=this.dropdownChanged >
                              <option value=JSON.stringify( name: 'name', key: 'key', type: "ALL" ) >All Projetcs and Spaces</option></select>





                              share|improve this answer

























                              • Add some more explanation to your solutions.

                                – Manmohan_singh
                                Mar 26 '18 at 14:10













                              0












                              0








                              0







                              Try this one:



                              dropdownChanged = (event) => 
                              let obj = JSON.parse(event.target.value);
                              this.setState(

                              key: obj.key,
                              selectedName: obj.name,
                              type: obj.type
                              );



                              <select onChange=this.dropdownChanged >
                              <option value=JSON.stringify( name: 'name', key: 'key', type: "ALL" ) >All Projetcs and Spaces</option></select>





                              share|improve this answer















                              Try this one:



                              dropdownChanged = (event) => 
                              let obj = JSON.parse(event.target.value);
                              this.setState(

                              key: obj.key,
                              selectedName: obj.name,
                              type: obj.type
                              );



                              <select onChange=this.dropdownChanged >
                              <option value=JSON.stringify( name: 'name', key: 'key', type: "ALL" ) >All Projetcs and Spaces</option></select>






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 26 '18 at 14:13

























                              answered Mar 26 '18 at 14:07









                              Slavi VatahovSlavi Vatahov

                              11




                              11












                              • Add some more explanation to your solutions.

                                – Manmohan_singh
                                Mar 26 '18 at 14:10

















                              • Add some more explanation to your solutions.

                                – Manmohan_singh
                                Mar 26 '18 at 14:10
















                              Add some more explanation to your solutions.

                              – Manmohan_singh
                              Mar 26 '18 at 14:10





                              Add some more explanation to your solutions.

                              – Manmohan_singh
                              Mar 26 '18 at 14:10











                              0














                              You can actually find the selectedOptions inside the target... no need to iterate over all the options. Let's imagine you want to send the values to an onChange function passed to your component as props: you can use the following function on the onChange of your multiple select.



                              onSelectChange = (e) => 
                              const values = [...e.target.selectedOptions].map(opt => opt.value);
                              this.props.onChange(values);
                              ;





                              share|improve this answer

























                              • At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                                – Inkling
                                Mar 9 at 1:09















                              0














                              You can actually find the selectedOptions inside the target... no need to iterate over all the options. Let's imagine you want to send the values to an onChange function passed to your component as props: you can use the following function on the onChange of your multiple select.



                              onSelectChange = (e) => 
                              const values = [...e.target.selectedOptions].map(opt => opt.value);
                              this.props.onChange(values);
                              ;





                              share|improve this answer

























                              • At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                                – Inkling
                                Mar 9 at 1:09













                              0












                              0








                              0







                              You can actually find the selectedOptions inside the target... no need to iterate over all the options. Let's imagine you want to send the values to an onChange function passed to your component as props: you can use the following function on the onChange of your multiple select.



                              onSelectChange = (e) => 
                              const values = [...e.target.selectedOptions].map(opt => opt.value);
                              this.props.onChange(values);
                              ;





                              share|improve this answer















                              You can actually find the selectedOptions inside the target... no need to iterate over all the options. Let's imagine you want to send the values to an onChange function passed to your component as props: you can use the following function on the onChange of your multiple select.



                              onSelectChange = (e) => 
                              const values = [...e.target.selectedOptions].map(opt => opt.value);
                              this.props.onChange(values);
                              ;






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 8 at 15:07









                              Ru Chern Chong

                              2,17362030




                              2,17362030










                              answered Mar 8 at 14:49









                              LanciLanci

                              213




                              213












                              • At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                                – Inkling
                                Mar 9 at 1:09

















                              • At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                                – Inkling
                                Mar 9 at 1:09
















                              At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                              – Inkling
                              Mar 9 at 1:09





                              At the time this question was asked, selectedOptions didn't have good compatibility. It's still not supported by Internet Explorer, so not really usable if you want IE support (at least without a polyfill).

                              – Inkling
                              Mar 9 at 1:09

















                              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%2f28624763%2fretrieving-value-from-select-with-multiple-option-in-react%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







                              Popular posts from this blog

                              1928 у кіно

                              Захаров Федір Захарович

                              Ель Греко