Wednesday, May 19, 2010

How to delete all selected elements on a select with javascript

If you want to delete all selected elements in a HTML select component here a small javascript function that will do the job.

function deleteFromSelect(list){
  var i = 0;
  var limit = list.options.length;
  while(i < limit)  
  {
      if(!list.options[i].selected)
     {
         i++;
     }
     else
    {
        list.options[i] = null;
        limit--;
    }
  }
}

No comments:

Post a Comment