How to get selected items from <select multiple ... > using Javascript
While working for one of our products BookShelf, I was faced with a problem of getting the selected items from
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:
Lets look at a simple html code ...
User comments
Thanks for the input! I use to do this the same way (earlier) as you mentioned, but there is a slight change which has a huge performance implication.
But consider a situation. Lets say there are 1000 options in the select element and 5 are selected. Your code would traverse through all 1000 to get just those 5 selected. However my approach would only traverse through those selected 5 items.
I did some testing. Made a drop down containing 10,000 items and used both methods described above to determine which is better. Came up with the following:
Method 1: while(selected index>-1)
Method 2: for(every element) if(selected)
Method 1 worked faster for a very low percentage (only one or two) of selected elements. Biggest downside is that it clears the selections and runs slower for more selected items. Restoring the selection after running this method took longer than method 2 in almost all cases
Method 2, for a list of 10,000 items, only took about a second to complete (2.00 GHz processor)
For short lists (for example, 10 items or so) you won't notice a difference.
Basically, if you've got a page with a very large list of items of which only a few will be selected, and do not need to keep track of what was selected: (i feel sorry for the users that have to filter through that to find the two elements they want) Use method 1. You may want to consider revising the provided code by saving document.getElementById('dropdown_name').selectedIndex to a variable instead of calling it multiple times. This call does take time to run.
For everyone else, use Method 2. It actually runs faster for a higher percentage of selected items. It's also simpler to read and write.
Anyway, hope that's helpful to someone. If you feel the need to pay me for it, my email address is above :)
--
Milton
...i believe you have my stapler...
$('option:selected',selobj).each( function() { vals.push( this.value ) } );
and if you needed even more space or the resulting string of select values is going to be real big consider using ajax and pass the items one at a time.
It's really low overhead.
Either way you only call the function once for each change? If this is dumb post a reply. I only started this stuff a few months ago.
egg
and if you needed even more space or the resulting string of select values is going to be real big consider using ajax and pass the items one at a time.
It's really low overhead.
Either way you only call the function once for each change? If this is dumb post a reply. I only started this stuff a few months ago.
egg
...The Above text will select the index which id is "myId".function getMultipleSelection(formName,elementName,array){ var selected = new Array(); var mySelect = document.forms[formName].elements[elementName]; for(i = 0; i < mySelect.options.length; i++) { if(mySelect.options.selected) { selected.push(mySelect.options.value); } } if(array != 'true') return selected.toString(); else return selected; }
function getMultipleSelection(formName,elementName,array){ var selected = new Array(); var mySelect = document.forms[formName].elements[elementName]; for(j = 0; j < mySelect.options.length; j++) { if(mySelect.options[j].selected) { selected.push(mySelect.options[j].value); } } if(array != 'true') return selected.toString(); else return selected; }
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
selected = new Array(); for (var i = 0; i < ob.options.length; i++) if (ob.options[ i ].selected) selected.push(ob.options[ i ].value);
This is actually one of my annoyances, so I was delighted to see that Web Forms 2.0 fixes it.Editor's note: The spaces between the options[ i ] are added to prevent it from conflicting it with the BBCode. It is not there in the original code.