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

August 3rd, 2004, 10:09 AM
|
Friend of Wrox
|
|
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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\' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.
|

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

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.
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |