![]()
Snippets of A |
|
|
What are snippets? |
|
select tag) that allows multiple selections of options, create a function that permits the multiple selections and deselections without keeping pressed the ctrl key on the keyboard, and also without having to loop all the options in order to achieve the goal (which may cause significant delays in case of menus that include a few hundreds of options, which may happen).function selections(sel){ if(!sel['storage']){sel['storage']={};};if(sel['size']>sel.options.length){sel['size']=sel.options.length;};if(sel.selectedIndex<0){return;}; var currentIndex=sel.selectedIndex;sel.selectedIndex=-1; if(!sel['storage'][currentIndex]){sel['storage'][currentIndex]=true;}else{delete sel['storage'][currentIndex];} for(var i in sel['storage']){sel.options[i].selected=true;} /*keep this comment to reuse freely http://www.fullposter.com/?1*/}Remove colors
this.
onclick event on the given menu.
onclick events (neither onmouseup or onmousedown or any other similar events) set on the options (no matter whether hardcoded or dynamically assigned); therefore the function relies on the selectedIndex property as passed to the select object, and yet on a multiple selection menu such property would always return only the topmost selected option index: therefore the function stores all the given selections (de-selections being, of course, another click onto an already selected option) and resets to -1 the selectedIndex as soon as it has read the latest; this means that, actually, the function reads each time a menu without any previous option selected (sel.selectedIndex=-1) except the current one, and as soon as it has done this, it reassigns all the previous selections. This may lead to ultrafast flashes of the menu.
this.
size of the menu is higher than the amount of options it accommodates; to this purpose, at each run the function resizes the menu accordingly to be sure this possibility never occurs.
storage (which must not be overwritten by other subroutines): this is an associative array. A 'regular' numerically indexed array object would have not fit, for upon assigning to a regular array any numerical index it would automatically produce an array whose length is given by the higher index assigned (which may lead to long arrays populated with nonsensical empty entries that nonetheless would demand of being scanned via a throughout loop).
<script> //include the function! </script> <form> <select multiple="multiple" size="8" onclick="selections(this);"> <option value="0">zero</option> <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> <option value="6">six</option> </select> </form>Remove colors