Blog » Javascript

How to get selected items from <select multiple ... > using Javascript

17 July 2004, 12:06 am ISTFiled under: Javascript

While working for one of our products BookShelf, I was faced with a problem of getting the selected items from

<select multiple='multiple' ... > </select>

tag using JavaScript. After thinking, figured out a simple way to do it. The trick was to make the de-select the selected items and keep them adding to an array for further processing. Here is the JavaScript code:

var arSelected = new Array(); function getMultiple(ob) { while (ob.selectedIndex != -1) { if (ob.selectedIndex != 0) arSelected.push(ob.options[ob.selectedIndex].value); ob.options[ob.selectedIndex].selected = false; } // You can use the arSelected array for further processing. }

Lets look at a simple html code ...

<form name='frmSelect'> <select name='numbers' multiple='multiple' onblur='getMultiple(document.frmSelect.numbers);'> <option value='1'>One</option> <option value='2'>Two</option> <option value='3'>Three</option> <option value='4'>Four</option> <option value='5'>Five</option> </select> <input type='submit' value='Submit' onclick='return confirm("You have selected: " + arSelected.toString();' /> </form>


Add to MyMSN
Add to MyYahoo
Add to Google



Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter

Get Firefox

Monitored by Site24x7