On Mar 3, 6:01 am, wavded <wav...@gmail.com> wrote: > OK this got me a significant performance bump and works in IE (there is like > 5800 options in this multi select, don't ask me why):
Perhaps you should. 5,800 options in a select sounds like a ridiculous number, how do users find a particular option among that many? > Seems caching the > elements themselves instead of the element values did the trick. > > var csubs = null; > $('select[name="Subdivision"]').change(function(){ > var tsubs = $(this).val(); > if(tsubs.length > 10){ > alert('Cannot select more than 10'); > $(this).find('option:selected').attr('selected',false); > csubs.attr('selected',true); > } else { > csubs = $(this).find('option:selected'); > } > }); It would be more efficient and much more user friendly to let the user de-select options until only 10 are selected. Then you only need to count how many are selected, no need to unselect/reselect or remember previously selected options. The logic is very much simpler, an option counting function that runs over 6,000 options in IE in 30ms can be as simple as: function countSelected(el) { for (var i=0, c=0, iLen=el.length; i<iLen; i++) { c += el.options[i].selected? 1 : 0; } return c; } Presumably you only care that 10 are selected when the form is submitted (or whatever equivalent action you have). So the fact that at some time or other the user selected some number greater than that is of no interest to you. Using alerts for error messages is also user un-friendly. Provide a hint that only 10 should be selected, then put a count in the page so they know how many are selected. Present an in-page error if they go over 10, so they can choose which ones to de-select. Imagine a user who selects 10 options at the very top, then goes to near the bottom to select one more. It would be much nicer if they can be warned they've selected 11, and can (if they want) return to the top and deselect one. With the posted code, they may have to go to the top and deselect one, then go back to the bottom to select the one that was programmatically deselected without prior warning. -- Rob -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com