1
Would someone like to tell me why this command line isn’t working:
$("#marcas_select + #modelos_teste").css("background-color", "yellow");
Here’s the full code. As everyone can see the goal is to let the select model_test with the yellow background
$(document).on('change', ':focus', function(evt) {
$("#marcas_select + #modelos_teste").css("background-color", "yellow");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group nested-fields" style="border: 1px solid red">
<div class="field col-xs-3">
<label for="person_carros_attributes_0_marca_id">Marca</label>
<select id="marcas_select" class="form-control" name="person[carros_attributes][0][marca_id]"><option value="">Selecionar marca</option>
<option value="1">Fiat</option>
<option value="2">Chevrollet</option>
<option value="3">Ford</option>
</select>
</div>
<br></br>
<div class="field col-xs-3">
<label for="person_carros_attributes_0_modelo_id">Modelo</label>
<select id="modelos_teste" class="form-control" name="person[carros_attributes][0][modelo_id]"><option value="6">Uno</option>
<option value="7">Palio</option>
<option value="8">Siena</option>
<option value="9">Prisma</option>
<option value="10">Onix</option>
<option value="11">Cobalt</option>
<option value="12">Ka</option>
<option value="13">Toro</option>
</select>
</div>
I will have several select with id tags_select so I think this: "#tags_select:Focus + #modelos_test" would be more appropriate, only it is not working
– Erasmo Santos
It is not a good practice to repeat IDS, they are unique use classes for this type of model, and I think you do not understand how the selector works, it will never work because between the id selects_select has "</div> <br></br> <div class="field col-Xs-3"> <label for="person_carros_attributes_0_modelo_id">Model</label>" they are not even in the same scope.
– Marco Vinicius Soares Dalalba
In fact repeated ID cause several @Erasmosantos bugs, it is totally impractical. If you need the same selector for multiple fields, use equal attributes or classes if applicable.
– BrTkCa