Tip: use debugger to see what are the values of variables that you use.
Basically, your code does the following (assuming you choose, e.g., main_4
from first menu):
on
$('#main').change()
you remove all 12<option>
elements from#sub0
.you pass those elements (still 12) to
show_opt
and filter them, so only 3 remain (sub0_4.1
etc.).you append those 3 elements to
#sub0
. Now#sub0
has only 3<option>
elements.on next
change()
there are only 3 elements in#sub0
. Assume you choosemain_3
this time - after youfilter()
those 3<option>
elems inshow_opt
, none remain.
Hope now it's clear to you why your code doesn't work.