[html] Post values from a multiple select

How can I post values from a multiple select in a form? When I hit submit none of the selected values are posted.

<form id="form" action="" method="post">
    <div>
        <select id="inscompSelected" multiple="multiple" class="lstSelected">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>                
        </select>
        <input type="submit" value="submit">
    </div>
</form>

This question is related to html html-select

The answer is


try this : here select is your select element

let select = document.getElementsByClassName('lstSelected')[0],
    options = select.options,
    len = options.length,
    data='',
    i=0;
while (i<len){
    if (options[i].selected)
        data+= "&" + select.name + '=' + options[i].value;
    i++;
}
return data;

Data is in the form of query string i.e.name=value&name=anotherValue