SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    For Loop Problems!?!?

    Hello all! I am having a problem with a little loop and was wondering if anone could exsplain what my problem is?

    Code:
    <html>
    <head>
    <script>
    var dp=false;
    function getDrives(frm)
    	{
    	var drvs, drv, fObj=new ActiveXObject("Scripting.FileSystemObject");
    	drvs=new Enumerator(fObj.Drives);
    	   if(!dp)
    	   {
    		for(;!drvs.atEnd();drvs.moveNext())
    		{
    			drv=drvs.item();
    				if(drv.IsReady)
    				{
    		 			var opt=document.createElement("option");
    					var txt=document.createTextNode(drv.DriveLetter+":"+drv.VolumeName);
    					opt.setAttribute("name",drv.DriveLetter);
    					opt.setAttribute("value",drv.DriveLetter+":\\");
    					opt.appendChild(txt);
    			 		frm.appendChild(opt);
    				}	
    				else{
    					var opt=document.createElement("option");
    					var txt=document.createTextNode(drv.DriveLetter+": (No Disk)");
    					opt.setAttribute("name",drv.DriveLetter);
    					opt.setAttribute("value",drv.DriveLetter+":\\");
    					opt.appendChild(txt);
    			 		frm.appendChild(opt);
    				}
    		dp=true;
    		}
    	   }
    	}
    
    function clr(frm)
    	{
    		for(i=0;i<frm.options.length;i++)
    		{
    			frm.removeChild(frm.options[i]);
    		}
    	}
    
    </script>
    </head>
    <body>
    <form name="theFrm">
    <table>
    
    <tr>	
    	<td>
    	<input type="button" value="Get Drives" onclick="getDrives(this.form.theDrives)" style="width:125;"><br>
    	<select name="theDrives" size="10" style="width:125;"> 
    	</select>
    	<input type="button" value="Clear" onclick="clr(this.form.theDrives)" style="width:125;"><br>
    	</td>
    </tr>
    
    </table>
    </form>
    </body>
    </html>
    The function I have highlighted is causing the problem! Basically I just want it to remove all the options and leave an empty select box! But when I click the button it removes the first three then one more each time you click it after that! I just want it to remove all of the children of the select box with one click! Am I doing it the wrong way or asking for to much! I am baffled by this problem?

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    As the drives are getting removed from the list, "frm.options.length" decreases. To resolve this problem, assign "frm.options.length" to a variable. The following alerts in the function illustrates the problem.

    Code:
    function clr(frm)
    {
         // Checks the original length.
         alert(frm.options.length);
    
         for(i=0;i<frm.options.length;i++)
         {
              // This length decreases as each drive is removed.
              alert(frm.options.length);
              frm.remove(frm.options[i]);
         }
    }
    SOLUTION:
    Code:
    function clr(frm)
    {
         driveLength = frm.options.length;
    
         for(i=0;i<driveLength;i++)
         {
              frm.remove(frm.options[i]);
         }
    }
    --Daniel

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...don't waste your time on this. Cross-posted to another board, already answered, one of those people...
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •