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;
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
add a comment |
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
add a comment |
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
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
javascript dom reactjs
edited Mar 9 at 1:11
Inkling
asked Feb 20 '15 at 8:43
InklingInkling
1,49122028
1,49122028
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
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);
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 anHTMLOptionsCollection
, not an Array.
– TrueWill
Aug 8 '18 at 17:35
|
show 8 more comments
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);
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, sinceselectedOptions
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
add a comment |
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.slice
and 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.
2
As mentioned in one of my other comments, browser support forselectedOptions
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
add a comment |
Easiest way...
handleChange(evt)
this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));
add a comment |
The following worked for me
var selectBoxObj = React.findDOMNode(this.refs.selectBox)
var values = $(selectBoxObj).val()
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
add a comment |
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
Any ideas on how to combine the selected options from multipleselect
s into one array? Say after grabbing them all viadocument.querySelectorAll('select')
?
– Casimir
Apr 29 '18 at 16:17
add a comment |
Another way to do it:
handleChange( target )
this.props.someCallback(
Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
)
add a comment |
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>
Add some more explanation to your solutions.
– Manmohan_singh
Mar 26 '18 at 14:10
add a comment |
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);
;
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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 anHTMLOptionsCollection
, not an Array.
– TrueWill
Aug 8 '18 at 17:35
|
show 8 more comments
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);
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 anHTMLOptionsCollection
, not an Array.
– TrueWill
Aug 8 '18 at 17:35
|
show 8 more comments
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);
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);
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 anHTMLOptionsCollection
, not an Array.
– TrueWill
Aug 8 '18 at 17:35
|
show 8 more comments
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 anHTMLOptionsCollection
, 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
|
show 8 more comments
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);
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, sinceselectedOptions
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
add a comment |
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);
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, sinceselectedOptions
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
add a comment |
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);
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);
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, sinceselectedOptions
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
add a comment |
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, sinceselectedOptions
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
add a comment |
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.slice
and 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.
2
As mentioned in one of my other comments, browser support forselectedOptions
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
add a comment |
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.slice
and 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.
2
As mentioned in one of my other comments, browser support forselectedOptions
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
add a comment |
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.slice
and 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.
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.slice
and 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.
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 forselectedOptions
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
add a comment |
2
As mentioned in one of my other comments, browser support forselectedOptions
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
add a comment |
Easiest way...
handleChange(evt)
this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));
add a comment |
Easiest way...
handleChange(evt)
this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));
add a comment |
Easiest way...
handleChange(evt)
this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));
Easiest way...
handleChange(evt)
this.setState(multiValue: [...evt.target.selectedOptions].map(o => o.value));
answered Dec 19 '17 at 20:26
jamesmfriedmanjamesmfriedman
42555
42555
add a comment |
add a comment |
The following worked for me
var selectBoxObj = React.findDOMNode(this.refs.selectBox)
var values = $(selectBoxObj).val()
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
add a comment |
The following worked for me
var selectBoxObj = React.findDOMNode(this.refs.selectBox)
var values = $(selectBoxObj).val()
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
add a comment |
The following worked for me
var selectBoxObj = React.findDOMNode(this.refs.selectBox)
var values = $(selectBoxObj).val()
The following worked for me
var selectBoxObj = React.findDOMNode(this.refs.selectBox)
var values = $(selectBoxObj).val()
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
add a comment |
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
add a comment |
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
Any ideas on how to combine the selected options from multipleselect
s into one array? Say after grabbing them all viadocument.querySelectorAll('select')
?
– Casimir
Apr 29 '18 at 16:17
add a comment |
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
Any ideas on how to combine the selected options from multipleselect
s into one array? Say after grabbing them all viadocument.querySelectorAll('select')
?
– Casimir
Apr 29 '18 at 16:17
add a comment |
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
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
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 multipleselect
s into one array? Say after grabbing them all viadocument.querySelectorAll('select')
?
– Casimir
Apr 29 '18 at 16:17
add a comment |
Any ideas on how to combine the selected options from multipleselect
s into one array? Say after grabbing them all viadocument.querySelectorAll('select')
?
– Casimir
Apr 29 '18 at 16:17
Any ideas on how to combine the selected options from multiple
select
s 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
select
s into one array? Say after grabbing them all via document.querySelectorAll('select')
?– Casimir
Apr 29 '18 at 16:17
add a comment |
Another way to do it:
handleChange( target )
this.props.someCallback(
Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
)
add a comment |
Another way to do it:
handleChange( target )
this.props.someCallback(
Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
)
add a comment |
Another way to do it:
handleChange( target )
this.props.someCallback(
Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
)
Another way to do it:
handleChange( target )
this.props.someCallback(
Array.prototype.slice.call(target.selectedOptions).map(o => o.value)
)
answered May 30 '17 at 8:42
Aral RocaAral Roca
1,86322447
1,86322447
add a comment |
add a comment |
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>
Add some more explanation to your solutions.
– Manmohan_singh
Mar 26 '18 at 14:10
add a comment |
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>
Add some more explanation to your solutions.
– Manmohan_singh
Mar 26 '18 at 14:10
add a comment |
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>
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>
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 a comment |
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 a comment |
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);
;
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
add a comment |
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);
;
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
add a comment |
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);
;
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);
;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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