473,511 Members | 15,715 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="DEPARTMENT" SIZE ="1" style="width:200px;"
onChange="Change()">
<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:200px;" 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="DEPARTMENT" VALUE="">
<INPUT TYPE="HIDDEN" NAME="CATEGORY" VALUE="">

function Change()
{
var selIndex = document.getElementById('DEPARTMENT').selectedInde x;
var selValue =
document.getElementById('DEPARTMENT').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 1583

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******@canada.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:15em;">
<select name="DEPARTMENT" size="1" onchange="Change(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:15em;">
<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.selectedIndex].value;
var opts = el.form.elements['CATEGORY'];
var i = opts.length;
while ( i-- ){
opts[i].style.display = (opts[i].value.match(dept))?'':'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.com/javascript/dynamicoptionlist/>
--
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.selectedIndex].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.selectedIndex].value;

// Get the collection of 'CATEGORY' options
var opts = el.form.elements['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.display = '';

} else {

// Otherwise hide it
opt.style.display = '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="DEPARTMENT" SIZE ="1" style="width:200px;"
onChange="Change()">
<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:200px;" 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="DEPARTMENT" VALUE="">
<INPUT TYPE="HIDDEN" NAME="CATEGORY" VALUE="">

function Change()
{
var selIndex = document.getElementById('DEPARTMENT').selectedInde x;
var selValue =
document.getElementById('DEPARTMENT').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.getElementById('DEPARTMENT')
var selIndex = dep.selectedIndex;
var selValue = dep.options[selIndex].value;
var catValue = selValue.substring(0,selValue.indexOf('.');
var cat = document.getElementById('CATEGORY');
for(var i=0;i<car.length;i++)
if(cat[i].value==catValue) {
cat[i].selected=true;
// or (as you prefer)
// cat.selectedIndex = 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.getElementById('DEPARTMENT');
var selIndex = dep.selectedIndex;
var selValue = dep.options[selIndex].value;
var cat = document.getElementById('CATEGORY');

for(var i=0;i<cat.length;i++)
{
var s = cat[i].value;
if( s.substring(0,s.indexOf('.')) == selValue )
{
cat[i].selected=true;
// or (as you prefer)
// cat.selectedIndex = 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.getElementById('DEPARTMENT');
var cat = document.getElementById('CATEGORY');
var selIndex = cat.selectedIndex;
var selValue = cat.options[selIndex].value;

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

====================
display only correct items in select 2
Change_and_display(true)
resvert to select 2
Change_and_display(false)
var SelDept = new Array();
var DeptViewed = false
function Change_and_display(yesnot)
{
var dep = document.getElementById('DEPARTMENT');
var cat = document.getElementById('CATEGORY');
var selIndex = cat.selectedIndex;
var selValue = cat.options[selIndex].value;
var Sl = new Array();
var j = 0;
for(var i=0;i<dep.length;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.length;
for(var i=0;i<Sl.lengh;i++) {
dep[i].value = Sl[i][0];
dep[i].text = Sl[i][0];
}
}
else {
dep.length=SelDept.length;
for(var i=0;i<SelDept.lengh;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_display(true)
resvert to select 2
Change_and_display(false)
OOoops ! there was an error !

var SelDept = new Array();
var DeptViewed = false
function Change_and_display(yesnot)
{
var dep = document.getElementById('DEPARTMENT');
var cat = document.getElementById('CATEGORY');
var selIndex = cat.selectedIndex;
var selValue = cat.options[selIndex].value;
var Sl = new Array();
var j = 0;
for(var i=0;i<dep.length;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.length;
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=SelDept.length;
for(var i=0;i<SelDept.lengh;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
4288
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...
10
4118
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...
10
7623
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...
5
3580
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...
6
4843
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...
5
2269
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...
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...
10
6553
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
3265
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 = ,,]...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
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,...
0
7430
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...
0
7517
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...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
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...
0
4743
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...
0
1581
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 ...
1
790
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.