473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to make onchange to list common lists

Hi

Please see the following simple script:

<TD align="Left" width="30%" >
<select name="DEPARTMEN T" SIZE ="1" style="width:20 0px;"
onChange="Chang e()">
<option value="0"> &lt;ALL&gt
<option value="1000012" >Food Sales
<option value="1000014" >Dessert Sales
<option value="1000015" >Liquor Sales
<option value="1000016" >Beer Sales
<option value="1000017" >Wine Sales
<option value="1000226" >Merchandise Sales
<option value="1000227" >Clothing Sales
....
</select>
</TD>

<TD align="Left" width="30%" >
<select name="CATEGORY" SIZE ="1" style="width:20 0px;" onChange="">
<option value="0"> &lt;ALL&gt
<option value="1000012. 1" >D - Classic Recipes
<option value="1000012. 2" >D - Carne & Pollo
<option value="1000026. 41" >D - Filled Pastas
<option value="1000011. 42" >D - Seafood
<option value="1000143. 1" >D - Salads
<option value="1000147. 2" >D - Pastas & Sauces
<option value="1000148. 4" >D - Daily Specials
<option value="1000049. 2" >L - Salads
....
</select>
</TD>

<FORM name="InputForm " id="InputForm" >
<INPUT TYPE="HIDDEN" NAME="DEPARTMEN T" VALUE="">
<INPUT TYPE="HIDDEN" NAME="CATEGORY" VALUE="">

function Change()
{
var selIndex = document.getEle mentById('DEPAR TMENT').selecte dIndex;
var selValue =
document.getEle mentById('DEPAR TMENT').options[selIndex].value;
.....???
my question is, how to modify the change(), so that whenever select 1
department, the category list will show those items which have a prefix
value as the selected department? (ex. value in department 1000012, has
2 category 1000012.1 and 1000012.2 in above case).
--
Thanks lots
John
Toronto

Jul 23 '05 #1
9 1590

What you're looking for is something like what I pasted a bit ago at
http://nopaste.php-q.net/144945 .
Danny

On Sun, 03 Jul 2005 11:25:29 -0700, john woo <jo******@canad a.com> wrote:

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #2
Thanks lots for the example, Danny.

But I think the data structure in my case is different from your
example.

I think in your example, every item in the 1st select associated with
one array data for the 2nd select; my case is that (sorry I'm not good
at javascript), all items are not arranged in array, what we can
classify them is from the common values between items in 1st select and
the items in 2nd select, and the primary issue is, the order of the
list in 2nd select is random.

Can you tell in such case, how to classify or convert the data
structure for the above example, so that it looks like items in the 2nd
select are all in arrays each of which associated with a item in 1st
select?

--
Tlhanks again

John

Jul 23 '05 #3
john woo wrote:
[...]

my question is, how to modify the change(), so that whenever select 1
department, the category list will show those items which have a prefix
value as the selected department? (ex. value in department 1000012, has
2 category 1000012.1 and 1000012.2 in above case).


Below is an example of how to go about it. Be careful, you seem to
have duplicate element names in different forms - why not have just one
form and one set of elements?

I've included 'this' in the onchange call so that you don't need to use
getElementById - saves a few clock cycles by not having to looking up
the form.

You are also better off to specify the widths of elements in 'em' or
such so that they will scale with the user's font size.

<form name="InputForm " id="InputForm" action="" style="width:15 em;">
<select name="DEPARTMEN T" size="1" onchange="Chang e(this)">
<option value="0"> &lt;ALL&gt
<option value="1000012" >Food Sales
<option value="1000014" >Dessert Sales
<option value="1000015" >Liquor Sales
<option value="1000016" >Beer Sales
<option value="1000017" >Wine Sales
<option value="1000226" >Merchandise Sales
<option value="1000227" >Clothing Sales
</select>
<select name="CATEGORY" SIZE ="1" style="width:15 em;">
<option value="0"> &lt;ALL&gt
<option value="1000012. 1" >D - Classic Recipes
<option value="1000012. 2" >D - Carne & Pollo
<option value="1000026. 41" >D - Filled Pastas
<option value="1000011. 42" >D - Seafood
<option value="1000143. 1" >D - Salads
<option value="1000147. 2" >D - Pastas & Sauces
<option value="1000148. 4" >D - Daily Specials
<option value="1000049. 2" >L - Salads
</select>
</form>
<script type="text/javascript">

function Change(el) {
if ( !el.style ) return; // If style object not supported, return
var dept = el[el.selectedInde x].value;
var opts = el.form.element s['CATEGORY'];
var i = opts.length;
while ( i-- ){
opts[i].style.display = (opts[i].value.match(de pt))?'':'none';
}
}
</script>
If you want to do a lot of this stuff, have a look at Matt Kruze's
"dynamic options list".

<URL:http://www.mattkruse.c om/javascript/dynamicoptionli st/>
--
Rob
Jul 23 '05 #4
Thanks lots.

I've tried the above example and the Matt Kruze's one. unfortunately
all of them only have the value without associated text. my data, ex.
in 1st select <option value="1000012" >Food Sales, contains a value and
its text, value is the ID for database, text for display to user.

I still get stuck on manupulating this kind of data structure.

--
John

Jul 23 '05 #5
john woo wrote:
Thanks lots.

I've tried the above example and the Matt Kruze's one. unfortunately
all of them only have the value without associated text. my data, ex.
in 1st select <option value="1000012" >Food Sales, contains a value and
its text, value is the ID for database, text for display to user.
You wanted to show only those options that matched the department
value, that's what the script does.

You can get the option text using the option's text property:

var deptText = el[el.selectedInde x].text;

I still get stuck on manupulating this kind of data structure.


Just explain what you want to do. One issue with the code that I
posted is that if a category option is selected, then a department, an
category may remain displayed that does not belong to the current
department. The code below fixes this (it selects option[0] if the
current selected option does not belong to the selected department).
function Change(el) {
// If style is not supported, leave
if ( !el.style ) return;

// Get the value of the selected department option
var dept = el[el.selectedInde x].value;

// Get the collection of 'CATEGORY' options
var opts = el.form.element s['CATEGORY'];

// Setup some variables
var opt, i = opts.length;

// For every option element
while ( i-- ){

// Save a reference to the option (a little more efficient)
opt = opts[i];

// If its value matches the selected department, show it
if ( opt.value.match (dept) ) {
opt.style.displ ay = '';

} else {

// Otherwise hide it
opt.style.displ ay = 'none';

// If this one is selected and isn't a match, make the
// '0' element selected
if ( opt.selected ) {
opts[0].selected = true;
}
}
}
}
--
Rob
Jul 23 '05 #6
ASM
john woo wrote:
Hi

Please see the following simple script:

<TD align="Left" width="30%" >
<select name="DEPARTMEN T" SIZE ="1" style="width:20 0px;"
onChange="Chang e()">
<option value="0"> &lt;ALL&gt
<option value="1000012" >Food Sales
<option value="1000014" >Dessert Sales
<option value="1000015" >Liquor Sales
<option value="1000016" >Beer Sales
<option value="1000017" >Wine Sales
<option value="1000226" >Merchandise Sales
<option value="1000227" >Clothing Sales
...
</select>
</TD>

<TD align="Left" width="30%" >
<select name="CATEGORY" SIZE ="1" style="width:20 0px;" onChange="">
<option value="0"> &lt;ALL&gt
<option value="1000012. 1" >D - Classic Recipes
<option value="1000012. 2" >D - Carne & Pollo
<option value="1000026. 41" >D - Filled Pastas
<option value="1000011. 42" >D - Seafood
<option value="1000143. 1" >D - Salads
<option value="1000147. 2" >D - Pastas & Sauces
<option value="1000148. 4" >D - Daily Specials
<option value="1000049. 2" >L - Salads
...
</select>
</TD>

<FORM name="InputForm " id="InputForm" >
<INPUT TYPE="HIDDEN" NAME="DEPARTMEN T" VALUE="">
<INPUT TYPE="HIDDEN" NAME="CATEGORY" VALUE="">

function Change()
{
var selIndex = document.getEle mentById('DEPAR TMENT').selecte dIndex;
var selValue =
document.getEle mentById('DEPAR TMENT').options[selIndex].value;
....???
my question is, how to modify the change(), so that whenever select 1
department, the category list will show those items which have a prefix
value as the selected department? (ex. value in department 1000012, has
2 category 1000012.1 and 1000012.2 in above case).


function Change(){
var dep = document.getEle mentById('DEPAR TMENT')
var selIndex = dep.selectedInd ex;
var selValue = dep.options[selIndex].value;
var catValue = selValue.substr ing(0,selValue. indexOf('.');
var cat = document.getEle mentById('CATEG ORY');
for(var i=0;i<car.lengt h;i++)
if(cat[i].value==catValu e) {
cat[i].selected=true;
// or (as you prefer)
// cat.selectedInd ex = i
}
}

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
Thanks lots. after modifying, the function now is

<script type="text/javascript">
function Change()
{
var dep = document.getEle mentById('DEPAR TMENT');
var selIndex = dep.selectedInd ex;
var selValue = dep.options[selIndex].value;
var cat = document.getEle mentById('CATEG ORY');

for(var i=0;i<cat.lengt h;i++)
{
var s = cat[i].value;
if( s.substring(0,s .indexOf('.')) == selValue )
{
cat[i].selected=true;
// or (as you prefer)
// cat.selectedInd ex = i
}
}
}
</script >

It seemed very close to what I want, but it just displayed one of the
items corresponded to 1st select, and the user can still select all the
items in the 2nd select. How can I further modify the Change(), so that
only those matching items in the 2nd select can be displayed? ex. if
the selected value is 1000012 in the 1st select, then only 1000012.1
and 1000012.2 can be shown in the 2nd select.

I've tried the Rob's example, applying cat[i].style.display= 'none', but
it didn't work well in my computer.
--
Thanks
Best Regards
John

Jul 23 '05 #8
ASM
john woo wrote:
Thanks lots. after modifying, the function now is

It seemed very close to what I want, but it just displayed one of the
items corresponded to 1st select, and the user can still select all the
items in the 2nd select.
My english is quite poor, don't understand exactly what you want :
I thought you wanted
to show something in 1st select from a choice in 2nd
And now you want its inverse : choice in 1 has to show in 2
(with same function ! on my idea : not very possible)

How can I further modify the Change(), so that only those matching items in the 2nd select can be displayed? ex. if
the selected value is 1000012 in the 1st select, then only 1000012.1
and 1000012.2 can be shown in the 2nd select.

I've tried the Rob's example, applying cat[i].style.display= 'none', but
it didn't work well in my computer.


==========
select correct items in select 2

function Change2()
{
var dep = document.getEle mentById('DEPAR TMENT');
var cat = document.getEle mentById('CATEG ORY');
var selIndex = cat.selectedInd ex;
var selValue = cat.options[selIndex].value;

for(var i=0;i<dep.lengt h;i++)
{
var s = dep[i].value;
if( s.substring(0,s .indexOf('.')) == selValue )
{
dep[i].selected=true;
// or (as you prefer)
// dep.selectedInd ex = i
}
}
}

=============== =====
display only correct items in select 2
Change_and_disp lay(true)
resvert to select 2
Change_and_disp lay(false)
var SelDept = new Array();
var DeptViewed = false
function Change_and_disp lay(yesnot)
{
var dep = document.getEle mentById('DEPAR TMENT');
var cat = document.getEle mentById('CATEG ORY');
var selIndex = cat.selectedInd ex;
var selValue = cat.options[selIndex].value;
var Sl = new Array();
var j = 0;
for(var i=0;i<dep.lengt h;i++)
{
var s = dep[i].value;
if(!DeptViewed) {
SelDept[i]= new Array();
SelDept[i][0] = dep[i].value;
SelDept[i][1] = dep[i].text;
}
if( s.substring(0,s .indexOf('.')) == selValue )
{
Sl[j] = new Array();
Sl[j][0] = dep[i].value;
Sl[j][1] = dep[i].text;
}
}
if(yesnot) {
dep.length=Sl.l ength;
for(var i=0;i<Sl.lengh; i++) {
dep[i].value = Sl[i][0];
dep[i].text = Sl[i][0];
}
}
else {
dep.length=SelD ept.length;
for(var i=0;i<SelDept.l engh;i++) {
dep[i].value = SelDept[i][0];
dep[i].text = SelDept[i][0];
}
}
}

not verified

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #9
ASM
ASM wrote:

=============== =====
display only correct items in select 2
Change_and_disp lay(true)
resvert to select 2
Change_and_disp lay(false)
OOoops ! there was an error !

var SelDept = new Array();
var DeptViewed = false
function Change_and_disp lay(yesnot)
{
var dep = document.getEle mentById('DEPAR TMENT');
var cat = document.getEle mentById('CATEG ORY');
var selIndex = cat.selectedInd ex;
var selValue = cat.options[selIndex].value;
var Sl = new Array();
var j = 0;
for(var i=0;i<dep.lengt h;i++)
{
var s = dep[i].value;
// 1st time function is launched -> fill up array of 2nd select
if(!DeptViewed) {
SelDept[i]= new Array();
SelDept[i][0] = dep[i].value;
SelDept[i][1] = dep[i].text;
}
if( s.substring(0,s .indexOf('.')) == selValue )
{
Sl[j] = new Array();
Sl[j][0] = dep[i].value;
Sl[j][1] = dep[i].text;
j++ ; // this line was forgotten
}
}
DeptViewed=true ;
if(yesnot) { // reduce 2nd select to items needed
dep.length=Sl.l ength;
for(var i=0;i<Sl.lengh; i++) {
dep[i].value = Sl[i][0];
dep[i].text = Sl[i][0];
}
}
else { // rewrite the 2nd select
dep.length=SelD ept.length;
for(var i=0;i<SelDept.l engh;i++) {
dep[i].value = SelDept[i][0];
dep[i].text = SelDept[i][0];
}
}
}

not verified

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4306
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i in l] but I was hoping for something more like l. Hilde
10
4136
by: bearophile | last post by:
This is my first Python program (it's an improvement of a recursive version by Luther Blissett). Given a list like this: , ]]] It produces the "flatted" version: I think this operation is quite important, and it's similar to the built-in Mathematica Flatten function: l = {{"a", 2, {}, {3, 5, {4}}}}
10
7637
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how the onchange event isn't called when mouse wheel scrolling between A3 and B1, but it works properly when scrolling between A1 and A2. E.g. ------------------------------------------
5
3592
by: Cheddar | last post by:
I'm having trouble creating a very simple running total of a few drop down list. I have three seperate dropdown lists, I want the user to simply select some numbers and have the total of the selected numbers displayed. I'm not much of a programmer and this is really just a experiment. Can anyone help me out here. I know the code below doesnt work (no form tags etx0 but I thought the logic was fine?
6
4859
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
5
2277
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the 'list/array' and to compare the existing list information to new lists i'm not sure if i need to import modules, or if the base python install i have is sufficient. an example, or pointer to examples would be good...
0
373
by: Mirovk | last post by:
I am trying to take actions using onchange with an option list without success. I am not able to fully populate the list without a bad bad behavior (the list is not getting populated) What I am looking for, is being able to call a sub procedure each time a new item in the list is selected (of course after it has been populated)
10
6568
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
2
3267
by: antar2 | last post by:
Hello, I am a beginner in python. following program prints the second element in list of lists 4 for the first elements in list 4 that are common with the elements in list 5 list4 = ,,] list5 =
0
8036
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2454
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.