To set data of an <input>
element using JavaScript, we assign the value and name of that element like this:
var form = document.createElement("form");
var element = document.createElement("input");
element.value=value;
element.name=name;
In the case of a <select>
where the multiple
attribute is present, how do I set the value of that select element? For instance, how would I set the value of the myselect
element below:
<form method="post" action="/post/" name="myform">
<select multiple name="myselect" id="myselect">
<option value="1">option1</option>
<option value="2">option2</option>
...
I tried to set the value by doing this myselect.value=[1,2]
however it does not work. After selecting option1
and option2
I expected that it returns [1,2]
, but it just returns "1".
<select>
, not get all options or the selected option. – Sundstrom