Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 3rd, 2004, 10:09 AM
Friend of Wrox
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listboxes and Arrays(Cot'd)...


If I select a choice with 5 elements in its array (these 5 elements populate a new listbox by dynamically assigning each array element as a new option) then I select a new choice with for example 3 elements in its array, I still have two of the old elements in the 2nd listbox! How do I dynamically remove unnecessary choices from a list box?

This is my script so far, but I get an infinite loop:

function nullify(){
while(document.form1.select1.options.length > 1){
function null1(){
document.form2.select2.options[1] = null;
Thanks In Advance
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
Old August 3rd, 2004, 10:43 AM
Friend of Wrox
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post

Hi Interrupt,

Give this a try...

<script type="text/javascript">
// array of options
var listBox2Options =    [    
                            ["option 1", "1", "option 2", "2", "option 3", "3", "option 4", "4", "option 5", "5"],
                            ["option 1", "1", "option 2", "2", "option 3", "3", "option 4", "4", "option 5", "5", "option 6", "6", "option 7", "7"]    

function LoadListBox2(pListBox1){
    var listBox2 = document.getElementById("ListBox2");
    while(listBox2.options.length > 0){
    var options = listBox2Options[pListBox1.selectedIndex];
    for(var i = 0; i < options.length; i += 2){
        listBox2.options.add(new Option(options[i], options[i + 1]));


<form id="myForm" name="myForm">
    <select id="ListBox1" name="ListBox1" onchange="LoadListBox2(this);">
        <option value="0">(choose)</option>
        <option value="1">one</option>
        <option value="2">two</option>

    <select id="ListBox2" name="ListBox2"></select>
Best regards,


Old August 10th, 2004, 11:03 PM
Authorized User
Join Date: Aug 2004
Location: Yogyakarta, D.I. Yogyakarta, Indonesia.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts

You declare a value of document.form1.select1.options.length > 1, this will make your statement loop if the value of the object you have declared is larger than 1. Of course you get an infinite loop, cause you have have more than 1 options. You should add a break; statement if a condition is fullfiled. With break you can stop your loop and go to the next line of statement.

Cheers, too.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Listboxes Sub-Listboxes and Table Linking MJB05 Access 4 September 20th, 2005 12:37 PM
Listboxes and Arrays interrupt Javascript How-To 1 August 2nd, 2004 01:55 PM
Filtering listboxes Bill Snow VB Databases Basics 0 February 5th, 2004 03:32 PM
Listboxes Alaric VS.NET 2002/2003 1 October 29th, 2003 11:54 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.