469,926 Members | 1,841 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

How to deselect the more selected options from combobox?

I have a combobox, in that i have taken list of districts so i have provide a facility to select max 5 districts. If user by mistake select more than that i have to give an alert and when user click on ok of the alert msg the the more select option have to deselect. How to do that i am tring on code. see below

Expand|Select|Wrap|Line Numbers
  1. function validateListBox()
  2. {
  3. //debugger;
  4. selectedArray=[];
  5.   var selObj = document.getElementById('type1');
  6.   var i;
  7.   var count = 0;
  8.   var no = 0;
  9.   for (i=0; i<selObj.options.length; i++) {
  10.     if (selObj.options[i].selected)
  11.     {
  12.       statelist = selObj.options[i].text;
  13.       count++;
  14.       selectedArray.push(statelist);
  15.  
  16.       }
  17.       if(count > 4)
  18.       {
  19.         //debugger;
  20.         alert("Select four "+type+" at a time only.");
  21.  
  22.         if(i==selObj.options.length-1)
  23.         {
  24.             for (i=selObj.options.length-1; i>0; i--) 
  25.             {
  26.                 if (selObj.options[i].selected)
  27.                 {    
  28.                     no++;
  29.                     if(no>4)
  30.                     {
  31.                         selObj.options[i].selected=false;
  32.                         return false;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         else
  38.         {
  39.         selObj.options[i].selected=false;
  40.         return false;
  41.         }
  42.       }
Oct 20 '10 #1
1 1862
Dormilich
8,652 Expert Mod 8TB
it doesn’t look wrong at first glance. what part of your code is not working?
Oct 20 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by TJ Walls | last post: by
1 post views Thread by ReidarT | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.