We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a <select multiple='multiple'>. When more than one value is selected, form-to-object only includes one of the selected values, not all of them.
<select multiple='multiple'>
form-to-object
This reproduces the error:
const formToObj = require('form-to-object') const formEl = document.querySelector('#theForm') const params = formToObj(formEl) console.log(params)
<form id='theForm' action='' method='post'> <input type='hidden' name='blah' value='something' /> <select multiple='multiple' name='theMulti'> <option value='First Choice' selected='selected'>First Choice</option> <option value='Second Choice' selected='selected'>Second Choice</option> <option value='Third Choice' >Third Choice</option> </select> </form>
{blah: "something", theMulti: "First Choice"}
I would expect something like:
{blah: "something", theMulti: ["First Choice", "Second Choice"]}
The text was updated successfully, but these errors were encountered:
I came up with a solution. If you replace all of your uses of el.value with the getValue function below, multi-selects will work:
el.value
getValue
function getValue(el) { if(el.type !== 'select-multiple') return el.value if(!el.options || !el.options.length) return el.value const results = [...el.options].reduce(function(acc, option) { if(option.selected) { acc.push(option.value || option.text) } return acc }, []) return results.length ? results : undefined }
Sorry, something went wrong.
bfb853e
My apologies for the late reply, this looks great. Thanks!
No branches or pull requests
Using a
<select multiple='multiple'>
. When more than one value is selected,form-to-object
only includes one of the selected values, not all of them.This reproduces the error:
Javascript
HTML
Output
Expected
I would expect something like:
The text was updated successfully, but these errors were encountered: