0
I have this code here: https://jsfiddle.net/pedroguilherme/ka80cruz/2/
js
var cidades = [
'São Paulo',
'Rio de Janeiro',
'Bahia'];
cidades.forEach(function(item){
$('#cidades').append('<option>' + item + '</option>');
});
var test = [
'Test1',
'Test2',
'Test3'];
test.forEach(function(item){
$('#test').append('<option>' + item + '</option>');
});
html
<select id="cidades" class="cidades"></select>
<select id="test" class="test"></select>
Important I want when selecting the Sao Paulo option to include only within the other select... which is the select with id test a Test option 1.
And I want to select the option Rio de Janeiro include only within the other select... which is the select with id test a option Test 2.
And I want to select the option Porto Alegre include only within the other select... which is the select with id test a option Test 3.
- Append doesn’t solve,
- I can’t do it via foreach,
- I tried map and could not.
- I tried : select with change did not work.
- I tried change, "click", one etc did not work.
Returning.... :)
I wanted to do it like this
$('#saopaulo').change(function() {
$("#test1").html('<option>Menino Jesus</option>');
});
$('#riodejaneiro').change(function() {
$("#test2").html('<option>CDF Master</option>');
});
$('#portoalegre').change(function() {
$("#test3").html('<option>Ethos</option>');
});
You can use without value tb, but not with id.
– Sam
Without value you would take the option index and take the same index as the other array.
– Sam
In my answer, just change the
test[$(this).val()]
fortest[$("option:selected", this).index()]
.– Sam