Link to home
Start Free TrialLog in
Avatar of avim
avim

asked on

changing a <select> dropdown content...

Well, here goes:
i need to make 2 <select> boxes in a form
that when choosing one of the two first options the other select`s content should change AND ofcourse when choosing one of the other options(from the first box) there should be a different content in the second one....
Hmm.. that`s it.
Hope anyone could help me (and understand what i`m
 asking here to.... ;-)
TNX,
Avi.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Search on select and you will find at least 5 answers in the last month

Michel
Avatar of avim
avim

ASKER

tnx!
didn`t think aboy it....
btw, i see u alot out here and u helped
a few times  b4 too.
I realy appricate it!
:-)
Avatar of avim

ASKER

This question has a deletion request Pending
Avatar of avim

ASKER

This question has a deletion request Pending
<html>
<head>
<script language=javascript>
var m_arr = new Array();
m_arr[0] = "blah 1|blah 2";
m_arr[1] = "blah 3|blah 4";


function sh(obj, o_objn)
{
    f = obj.form;
    other_obj = f[o_objn];
      s = m_arr[obj.selectedIndex].split("|");
      for(i=0;i<s.length;i++)
      {
            other_obj.options[i] = new Option(s[i], s[i]);
      }
}
</script>
</head>
<body>
<form>
<select onChange="sh(this, 'otherselect')">
<option>menu 1</option>
<option>menu 2</option>
</select>

<select name="otherselect">
</select>
</form>
</body>
</html>
Avatar of avim

ASKER

Already found somtin`....
tnx again.
*LOL*...
here it is
<html>
<script language=javascript>
function c2()
{
  document.abc.s1.selectedIndex=document.abc.s2.selectedIndex;
  return true;
}

function c1()
{
  document.abc.s2.selectedIndex=document.abc.s1.selectedIndex;
  return true;
}

</script>

<body>
<form name=abc>
<select name=s1 onchange=c1()>
<option value=1>item 1
<option value=2>item 2
</select>
<select name=s2 onchange=c2()>
<option value=a>item a
<option value=b>item b
</select>
</body>
</html>

contact me for further enquiries
WAHAHHA =)...sorry, I'm having fun here. In seconds a thread is crowded with people...
Avatar of avim

ASKER

I`m happy if u`r happy! :-D
but realy, CJ_S , u made a great script but notice that when the page is first loaded there isn`t any content in the second select...
that could be confusing 4 the rookie user...

Avatar of avim

ASKER

I`m happy if u`r happy! :-D
but realy, CJ_S , u made a great script but notice that when the page is first loaded there isn`t any content in the second select...
that could be confusing 4 the rookie user...

the only line to change for that is:

<body>

would become

<body onload="sh(document.forms[0][0], 'otherselect')">

But if ur happy already.... :)

regards,
CJ
Avatar of avim

ASKER

I`m happy if u`r happy! :-D
but realy, CJ_S , u made a great script but notice that when the page is first loaded there isn`t any content in the second select...
that could be confusing 4 the rookie user...

Uhh - too many chefs!!!

Michel
Avatar of avim

ASKER

sorry CJ, althogh i`m happy there`s still  a little bug overthere....
try adding another option to the "menu2"
like so:
"....
     m_arr[1] = "blah 3|blah 4|blah 5";
........"
and than when running the page if u change your selection from menu1 to 2 and than back to menu 1 the "blah 5" is
still staying in there...
i guess the reason is pretty obvious  to u but i don`t know how to deal with that...
tnx alot,
avi.
:-)
Avatar of avim

ASKER

sorry CJ, althogh i`m happy there`s still  a little bug overthere....
try adding another option to the "menu2"
like so:
"....
     m_arr[1] = "blah 3|blah 4|blah 5";
........"
and than when running the page if u change your selection from menu1 to 2 and than back to menu 1 the "blah 5" is
still staying in there...
i guess the reason is pretty obvious  to u but i don`t know how to deal with that...
tnx alot,
avi.
:-)
sorry there : )

<html>
<head>
<script language=javascript>
var m_arr = new Array();
m_arr[0] = "blah 1|blah 2";
m_arr[1] = "blah 3|blah 4|blah 5";


function sh(obj, o_objn)
{
    f = obj.form;
    other_obj = f[o_objn];
      s = m_arr[obj.selectedIndex].split("|");
      other_obj.options.length = 0
      for(i=0;i<s.length;i++)
      {
            other_obj.options[i] = new Option(s[i], s[i]);
      }
}
</script>
</head>
<body onload="sh(document.forms[0][0], 'otherselect')">
<form>
<select onChange="sh(this, 'otherselect')">
<option>menu 1</option>
<option>menu 2</option>
</select>

<select name="otherselect">
</select>
</form>
</body>
</html>
Avatar of avim

ASKER

oh, and ....
the selections might change to more than 2 or 3 selections, so if u can help me understand how will i be able to deal with that script and adjust it as required later on...

ASKER CERTIFIED SOLUTION
Avatar of axis_img
axis_img

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Netscape will only show the number of items that were there when the drop  down was initially made.

Change

<select name="listTwo" ...>
<option>Sub Option List</option>
</select>

to

<select name="listTwo" ...>
<option>Sub Option List</option>
<option>_____________________________</option>
<option>_____________________________</option>
<option>_____________________________</option>
<option>_____________________________</option>
<option>_____________________________</option>
</select>

or rewrite a div with a form containing the select.

Also in select mutiple, you cannot make netscape scroll to a selection that is not visible
That's what I was assuming. I imagined that it came down to Netscape's inability to dynamically generate/regenerate an element once it has been displayed on the page. I had actually used the method of adding in filler spots so Netscape would not have a problem with it, but I was trying to find a work-a-round without having to put dummy spots in the select box... To no avail, apparently. :-)  

Thanks.. I am off to bed.

Cheers
The old method was to do a history.go(0) to refresh the select
<html>
<head>
<script language=javascript>
var m_arr = new Array();
m_arr[0] = "blah 1|blah 2";
m_arr[1] = "blah 3|blah 4|blah 5";


function sh(obj, o_objn)
{
    f = obj.form;
    other_obj = f[o_objn];
      s = m_arr[obj.selectedIndex].split("|");
      other_obj.options.length = 0
      for(i=0;i<s.length;i++)
      {
            other_obj.options[i] = new Option(s[i], s[i]);
      }
}
</script>
</head>
<body onload="sh(document.forms[0]['firstselect'], 'otherselect')">
<form>
<select onChange="sh(this, 'otherselect')" name='firstselect'>
<option>menu 1</option>
<option>menu 2</option>
</select>

<select name="otherselect">
</select>
</form>
</body>
</html>

m_arr is an array which contains all the menu-items corresponding to the first select-object. In the first select box you have the function onChange which takes 2 parameters. 1) the current object 2) the listbox which should be changed...the function itself gets some references to the form and objects. Then sets the second selectbox to nothing and then loops through the array with the index being the same as the current index of the select box! it splits the string as to get an array back and then loops through that one array and fills in the options. To adjust it to your likings ...well...the only thing for you (I think) would be to change the first selectbox with items you need...and to fill the array which contains the items for the other selectbox. Oh, and to get the current selected item in the second selectbox, you can just refer to it like normally.
document.forms[0]['otherselect'].value
or
document.forms[0]['otherselect'].selectedIndex

dunno what more to say...I hate my internetconnectiona t work =(...

regards,
CJ
Avatar of avim

ASKER

Thanks!
:-D