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>
Syndicate






Topics
- Ads (4)
- Apple (3)
- Envoirnment (1)
- Gadgets & Tools (11)
- Gaming (10)
- Google (14)
- Humor (15)
- Internet (1)
- Javascript (1)
- Management (3)
- Microsoft (23)
- Mobile (4)
- Mozilla (25)
- Music (3)
- Off the beat (9)
- Personal (0)
- PHP (6)
- Science (1)
- Social (3)
- Software (37)
- Spiritual (1)
- Technology (43)
- Videos (29)
- Web development (8)
About
Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Monitored by Site24x7
Uptime