• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

sticky multiple select box

The default properties of a multiple select form box are as follows:

click on an item in the select box and it becomes highlighted. Click on another item in the select box and the first item becomes un-highlighted. If you want to highlight two items at once, you need to hold down the ctrl button when making a second (or nth) selection.

I am trying to find a script that will allow the following:

click on an item in a multiple select box to highlight the item. click on another item in the select box and not only would it be selected, but the previous selection would also stay selected, thereby eliminating the need to hold down the ctrl button for multiple selections. However, if a highlighted item is clicked on again, it should become un-highlighted.

Has anyone seen a script for a multiple select box that can do this?

thanks for all your help.

-w

0
wfenton
Asked:
wfenton
2 Solutions
 
spiritwithinCommented:
Alright mate, i wrote the thing for you.

Paste it all into a blank html file, load up, and test. Here you go: (btw. i did ONLY test with MS Internet Explorer, you have to translate the code for other browsers yourself - if needed)

<html>

<script language="javascript" type="text/javascript">

// I do not know any script like that. However, i think it would be possible to do it. I do not comment here to get points, in this case i only want to help. So all i can do is tell you how it could be done:
// Make an Array:

/* TODO:
   Fill the array with as much zeroes, as your multiple
   select box has items. So if your selectbox contains for
   example 3 names, then add 3 zeroes to the array.
   If you work dynamically, for example with php, then do it
   like that: (note that $elementCount is the number of
               elements in the selectbox. If you do not
               know it at the time of placing the javascript
               into the file, then precalculate the selectbox
               contents, and set $elementCount to it's element
               Count.)
   <?php
    printf("var arSelect = new Array(");
    for ($iCtr = 0; $iCtr < $elementCount; $iCtr++) {
     printf("0");
    }
    printf(");");
   ?>
*/

var arSelect = new Array(0,0,0,0,0,0,0);

// Make a function:

function itemCheck(selectBox) {
 var iCtr = 0;
 for (iCtr = 0; iCtr < selectBox.options.length; iCtr++) {
  if (selectBox.options[iCtr].selected == true) {
   if (arSelect[iCtr] == 0) {
    arSelect[iCtr] = 1;
   } else {
    arSelect[iCtr] = 0;
   }
  }
 }
 arrayToSelectBox(selectBox);
}

function arrayToSelectBox(selectBox) {
 var iCtr = 0;
 for (iCtr = 0; iCtr < selectBox.options.length; iCtr++) {
  selectBox.options[iCtr].selected = false;
 }
 for (iCtr = 0; iCtr < arSelect.length; iCtr++) {
  if (arSelect[iCtr] == 1) {
   selectBox.options[iCtr].selected = true;
  }
 }
}

</script>

<body>
<form name="x" method="post" action="nothing">
 <select multiple style="width: 200px;" name="testBox" onchange="javascript:itemCheck(this);">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
 </select>
</form>
</body>
</html>
0
 
sybeCommented:
<script>
var arrMyDropDown = new Array();

function DropDownToArray() {
    for(var i=0;i<document.myform.myselect.options.length;i++) {
        arrMyDropDown[i] = false;
    }
}

function AdvancedSelect(j) {
     var oDropDown = document.myform.myselect;
     arrMyDropDown[j] = !arrMyDropDown[j];
    for(var i=0;i<oDropDown.options.length;i++) {
            oDropDown.options[i].selected=arrMyDropDown[i];
    }
}

</script>

<body onload="DropDownToArray()">

<form name="myform">
<select name="myselect" onChange="AdvancedSelect(this.options.selectedIndex);" multiple size="10">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
</select>


</form>
</body>

0
 
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: spiritwithin {http:#8045538} & sybe {http:#8047204}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now