Spooky Case of the Wem Ghost
Reading Email with PHP and IMAP/POP3
Installing Proprietary nVidia Drivers on Ubuntu Linux
Garmin Edge 205 GPS Cycle Training Tool
Nasa's Space Shuttle Atlantis Transiting the Sun
Javascript Snippets
 
 

Selected JavaScript Fun and Goodness

We need to make sure all the chosen options are selected before the form is submitted, for which we need a fifth function, selectAllOptions(). This is the usual function for selecting all options in a multiple select list, and it's typically fired by calling it via the form's onSubmit attribute. You don't need to worry about the state of the unselected list of course, as its contents are of no interest to us.

function selectAllOptions(selectId) {
   var selObj = document.getElementById(selectId);
   var len = selObj.options.length;
   for (var index=0; index<len; index++) selObj.options[index].selected = true;
}


The following function can be attached to a button and simply iterates through the options in the sourceSelect list displaying the label and value of each. It can be useful for testing. Note that it uses confirm() for displaying the option's properties rather than alert(), allowing us to choose whether to show the next option's properties or to break out of the loop:

function test(sourceSelect, targetSelect) {
   var len = sourceSelect.options.length;
   for (var index=0; index<len; index++)
      if (!confirm(sourceSelect.options[index].text+'--'+sourceSelect.options[index].value)) break;
}


Select All

Below is a function which will select all boxes in a specified table.

<script language="javascript">
   <!--
      function setallboxes() {
         var state = document.getElementById('setall').checked;
         var elements = document.getElementsByTagName('input');
         for (var offset = 0; offset < elements.length; offset++) {
            if (elements[offset].type == 'checkbox' && elements[offset].id.substr(0,6) == 'clear_') {
               elements[offset].checked = state;
            }
         }
      }
   -->
   </script>[/c]
1 2 3