Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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)...

Ok,

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){
null1()
}
function null1(){
document.form2.select2.options[1] = null;
}
}
</script>
Thanks In Advance
interrupt
__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
Reply With Quote
  #2 (permalink)  
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
Default

Hi Interrupt,

Give this a try...

Code:
<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){
        listBox2.remove(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]));
    }
}
</script>

...

<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>

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

Chris

Reply With Quote
  #3 (permalink)  
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
Default

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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.