Javascript listbox manipulation function collection summary

  • 2020-03-30 00:02:35
  • OfStack


<script language="javascript">

    

  //Description: add a non-repeating list box element
  function selAdd( srcList, dstList )
  {
      var selectedIndex = new Array();
      var count = 0;

      for ( i=0; i<srcList.options.length; i++ ){

         if ( srcList.options[i].selected ){

             selectedIndex[count] = i;
             count ++;

         }
     }                    

     for ( j=0; j<selectedIndex.length; j++ ){

         k = selectedIndex[j];

         if ( chkDup( srcList.options[k].value, dstList )==false ){
       &, nbsp;   dstList.options.length++;
             var len = dstList.options.length-1;
             dstList.options[len].value = srcList.options[k].value;
             dstList.options[len].text = srcList.options[k].text;
         }

     }

 }

 //Description: delete the list box element
 function selDel( list )
 {
     var len = list.options.length;
     var idx = 0;

     while ( idx< len ){

         if ( list.options[idx].selected ){
             list.options.remove(idx);
             len = list.options.length;
         }
         else{
             idx ++;
         }
     }
 }

 //Description: detects list box element duplication
 function chkDup( item, list )
 {
     for ( i=0; i<list.options.length; i++ ){
         //alert( item + " - " + list.options[i].value );
         if ( item == list.options[i].value ){
             return true;
         }
     }                    
     return false;
 }

 //Description: select all members of the list box
 function selSel( list, item )
 {
     item.value = " ";
     for ( i=0; i<list.options.length; i++ ){
         list.options[i].selected=true;
         item.value += list.options[i].value + " ";
     }

 }

 function selSelSingle( list, value )
 {
     for ( i=0; i<list.options.length; i++ ){
         if ( list.options[i].value == value ){
             list.options[i].selected=true;
             break;
         }
     }

 }
 //Description: initializes the list box according to the array
 function selList( item, arr )
 {

     var curIndex, insIndex, val, text;
     var arrItem = new Array();

     if ( item ){

         item.length = 0;
         curIndex = 0;

        for ( i=0; i<arr.length; i++ ){

             item.length ++;
            insIndex = item.length - 1;

            if ( arr[i] ){
                arrItem = arr[i].split( ", " );
                text = arrItem[1];
                val  = arrItem[0];
                item.options[ insIndex ].text = text;    
                item.options[ insIndex ].value= val;
            }
        }

    }
} 

    </script>

Related articles: