brainfoolong / form-data-json

A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
https://brainfoolong.github.io/form-data-json
MIT License
60 stars 10 forks source link

Populate multiple checkboxes with the same name (not using []-names) #41

Closed powerbuoy closed 1 month ago

powerbuoy commented 1 month ago

I have a form with multiple checkboxes, all named test (unfortunately, out of my control, not named test[]).

<input type="checkbox" name="test" value="foo">
<input type="checkbox" name="test" value="bar">

When trying to populate them using data like this {"test": ["foo", "bar"]} nothing happens.

Also, when fetching the form data as JSON I also only get the first checkbox.

Is there any way I can get around this problem?

https://codepen.io/powerbuoy/pen/KKOMBee

(NOTE: Changing the name to test[] resolves the issue, but again, is impossible for me to do in this case).

powerbuoy commented 1 month ago

Well I shouldn't have said impossible, because after some more tinkering I indeed managed to stick [] in the name attribute. But could still be useful to know whether or not this is doable without changing the name?

brainfoolong commented 1 month ago

Hi. This is not possible with this library. The array syntax is required to make it an array.