How to deselect the more selected options from combobox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rajesh Gite

    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

    Code:
    function validateListBox()
    {
    //debugger;
    selectedArray=[];
      var selObj = document.getElementById('type1');
      var i;
      var count = 0;
      var no = 0;
      for (i=0; i<selObj.options.length; i++) {
        if (selObj.options[i].selected)
    	{
          statelist = selObj.options[i].text;
    	  count++;
    	  selectedArray.push(statelist);
    
    	  }
    	  if(count > 4)
    	  {
    		//debugger;
    		alert("Select four "+type+" at a time only.");
    
    		if(i==selObj.options.length-1)
    		{
    			for (i=selObj.options.length-1; i>0; i--) 
    			{
    				if (selObj.options[i].selected)
    				{	
    					no++;
    					if(no>4)
    					{
    						selObj.options[i].selected=false;
    						return false;
    					}
    				}
    			}
    		}
    		else
    		{
    		selObj.options[i].selected=false;
    		return false;
    		}
    	  }
    Last edited by Dormilich; Oct 20 '10, 11:32 AM. Reason: please use [code] [/code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it doesn’t look wrong at first glance. what part of your code is not working?

    Comment

    Working...