Results 1 to 6 of 6

Thread: Javascript: Select Options [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Javascript: Select Options [Resolved]

    Hi,

    I've 2 select boxes. When the user changes the option in the first select box, the options in the second select box will change/populated.
    I used the onchange event on the first select box, but the options in the second select box is not populated.

    My codes:
    Code:
    <html>
    
    <head>
    <script language="Javascript">
    
    var NW = new Array("Network/User Admin", "Hardware", "Software");
    var BA = new Array("Email", "Intranet");
    
    function main_change(mymain) {
    	var _main = mymain.value;
    	
    	if (_main == "NW") {
    		
    		for (var k=0; k< NW.length; k++) {
    
    			OPT = new Option;
    			OPT.Text= NW[k];
    			OPT.Value= NW[k];
    			
    			document.forms['issue']['subCat'].options[k] = OPT;
    			
    		}
    
    				
    	}
    	else if (_main == "BA") {
    
    		for (var k=0; k< BA.length; k++) {
    
    			OPT = new Option;
    			OPT.Text= NW[k];
    			OPT.Value= NW[k];
    			
    			document.forms['issue']['subCat'].options[k] = OPT;
    			
    		}
    
    	}
    
    	
    }
    </script>
    
    </head>
    <body>
    <form name="issue">
    <table>
    <tr>
    		<td>Main Category:</td>
    		<td width=10>	<select name="main" onchange="main_change(this);">
    				<option>Please Choose One...</option>
    				<option value="NW">Network</option>
    <option value="BA">Business App</option>				
    			</select>
    		</td>
    		<td>Sub Category:</td>
    		<td>	<select name="subCat">
    				<option>Please Choose One...</option>
    		
    			</select>
    		</td>
    	</tr>
    
    </table>
    </form>
    
    </body>
    </html>
    Last edited by velvetskies; Aug 17th, 2004 at 07:21 PM.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    its the length part not returning the number of array sections.

    I'm looking into it a little more.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Info on array

    Ok, looking here it appears to be a problem setting the array.
    I've changed the code a little to assign values to the array, but for some reason it isn't setting them in the loop, almost as though its not seeing the variables...


    Vince

    Code:
    <html>
    
    <head>
    <script language="Javascript">
    
    var NW = new Array(3);
    NW[0]="Network/User Admin"
    NW[1]="Hardware"
    NW[2]="Software"
    var BA = new Array(2);
    BA[0]="Email"
    BA[1]="Intranet"
    alert(BA.length);
    
    function main_change(mymain) {
    	var _main = mymain.value;
    
    	if (_main == "NW") {
    		
    		for (var k=0; k< NW.length; k++) {
    
    			OPT = new Option;
    			OPT.Text= NW[k];
    			OPT.Value= NW[k];
    			
    			document.forms['issue']['subCat'].options[k] = OPT;
    			
    		}
    
    				
    	}
    	else if (_main == "BA") {
    
    		for (var k=0; k< BA.length; k++) {
    
    			OPT = new Option;
    			OPT.Text= NW[k];
    			OPT.Value= NW[k];
    			
    			document.forms['issue']['subCat'].options[k] = OPT;
    			
    		}
    
    	}else{
    		alert("None");
    	}
    
    	
    }
    </script>
    
    </head>
    <body>
    <form name="issue">
    <table>
    <tr>
    		<td>Main Category:</td>
    		<td width=10>	<select name="main" onchange="main_change(this);">
    				<option>Please Choose One...</option>
    				<option value="NW">Network</option>
    <option value="BA">Business App</option>				
    			</select>
    		</td>
    		<td>Sub Category:</td>
    		<td>	<select name="subCat">
    				<option>Please Choose One...</option>
    		
    			</select>
    		</td>
    	</tr>
    
    </table>
    </form>
    
    </body>
    </html>

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Hmm - seems to be how you are resetting the combo list now... not working.

    Good luck in finding the result


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70
    Hi Ecniv,

    Thanks for the reply!

    Changing the way of assigning values to the array doesnt seem to have a difference between the outputs of my codes and yours.

    i've checked out the options' values by..
    alert(document.forms['issue']['subCat'].options[a].Value);

    and they're populated with the array values (both ways of assigning array values)

    I'll contd to find the way out..


    But anyway, appreciate it. thanks again

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70
    Haha.. it should be

    OPT = new Option(NW[k], NW[k]);

    Yay!

Posting Permissions

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



Click Here to Expand Forum to Full Width