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
|