Blog » Javascript
How to get selected items from <select multiple ... > using Javascript
16 July 2004, 2:36 pm 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>
Syndicate






Topics
- Ads (3)
- Apple (3)
- Envoirnment (1)
- Gadgets & Tools (4)
- Games (7)
- Google (10)
- Humor (8)
- Javascript (1)
- Microsoft (23)
- Mozilla (25)
- Music (3)
- PHP (6)
- Software (33)
- Technology (32)
- Videos (10)
- Web development (5)
About
Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

