I have 2 chained select : when a value is choose in the first one, the second select show some values that correspond to the first select.
Example :
<select id='first_select'>
<option value='1' class='1'> Half Life </option>
<option value='2' class='2'> Mario </option>
</select>
<select id='second_select'>
<option class='1'> Gordon </option>
<option class='1'> Alexia </option>
<option class='2'> Peach </option>
<option class='2'> Luigi </option>
</select>
I have a filter input for the second select : when I type a letter/word, it show all values that have this letter/word.
The filter input search in ALL VALUES : For example, if I enter 'e' it will return 'Alexia' and 'Peach' !
I would like that the filter input search only in the list of values available that correspond to the value selected in the first select : If I select 'Mario' and I type 'e' I would like to have only the value 'Peach' !
I have a jQuery function which work but only when I reload the page with a first value selected (with cookie): (it create a new list/option with only these that are the same value/class)
$(document).ready( function() {
$('#first_select').change(function(){
var identif = jQuery('#first_select option:selected').attr('class');
// alert(identif);
var opts = $('#second_select option[class^='+identif +']').map(function(){
// alert(this.value);
return [[this.value, $(this).text()]];
});
$('#someinput').keyup(function(){
var rxp = new RegExp($('#someinput ').val(), 'i');
var optlist = $('#second_select').empty();
opts.each(function(){
if (rxp.test(this[1])) {
optlist.append($('<option />').attr('value', this[0]).text(this[1]));
}
});
});
});
});
Problem is that didn't work without reload the page, as soos as I change the first value, my new list stay empty..
EDIT :
When I put some alert, here what I have : When the page is load with a selected value : alert('1') then : alert('Gordon') and alert('Alexia'); but when I select a new value in the first select : alert('2') and that's all, whereas I must have alert('Peach') and alert('Luigi')
Thanks for your help and advices,
$('#someinput').keyup
listener outside the$('#first_input').change
. You can change a boolean value inside the$('#first_input').change
and check that one inside$('#someinput').keyup
. Maybe this solves your problem too, I don't know, just fix this first maybe. Because each time the first listener gets called the second one is re-defined, that's not good. – rednaw May 29 '13 at 9:09