-4

How do I use javascript to append the value of an input when it has the attribute "multiple"?

i.e. <input type='hidden' name='test[]' multiple>

So like how you would set the value of a normal input document.getElementById("myId").value = "whatever"; but to work with an input that has the attribute 'multiple'

James Driver
  • 18
  • 1
  • 6
  • I have no clue what you are asking. Your question needs more details. I assume it should be `type="file"` – epascarello Mar 31 '20 at 20:22
  • Sorry, what? `multiple` is only valid for ``. And using Vanilla JS you can set/unset attributes assigning the value of the property directly `element.multiple=true|false`. – Gabriel Mar 31 '20 at 20:25
  • 1
    @Gabriel [`multiple` is valid for `input type="file"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#multiple) – Heretic Monkey Mar 31 '20 at 20:26
  • Multiple on an input only applies to file or email fields [MSDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmultiple). But like @Gabriel said you probably need ` – Jeroen Bourgois Mar 31 '20 at 20:27
  • Yes, sorry, my bad, I added it before you pointed it. – Gabriel Mar 31 '20 at 20:27
  • I am trying to set the value of the input, not the attribute. By set, I mean append because it is a 'multiple' input. – James Driver Mar 31 '20 at 20:28
  • So, an `input type="hidden"` does not have the ability to set multiple values. So just set the `value` property of the `input` element. If you have actual code where you're trying that and it is failing, show us that code. – Heretic Monkey Mar 31 '20 at 20:32
  • Setting multiple values of a `select` is covered in [How to set values of multiple select using JavaScript](https://stackoverflow.com/q/55486020/215552) however. – Heretic Monkey Mar 31 '20 at 20:33

2 Answers2

0

I am trying to set the value of the input, not the attribute. By set, I mean append because it is a 'multiple' input

The multiple attribute does not apply to hidden inputs.

If you want to append to the value, then read the current value, and include it in the new value.

input.value = input.value + "some other string";

Since you've used PHP-style naming conventions, possibly you want to create an additional input:

const newInput = document.createElement("input");
newInput.name = "test[]";
newInput.type = "hidden";
newInput.value = "some other string";
input.insertAdjacentElement("afterend", newInput);

So the submitted data will be treated as an array of values in whatever is processing it once submitted.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Try this:


<select id="select">
    <option>...</option>
</select>

<script defer>
    document.querySelector("#select").setAttribute("multiple", "multiple")
</script>
Mark Minerov
  • 309
  • 1
  • 8
  • I am trying to set the value of the input, and by set I mean append because it has the attribute 'multiple'. Sorry if the question was vague. I edited it to hopefully reflect this better – James Driver Mar 31 '20 at 20:29