I have a select option field and would like the user to click submit, the system would color the
When inspecting the element with chrome, I noticed that the chosen one put a style display: none and if it does it works, but that's not what I want:
The object is empty if you click the button, the select option will turn red
Here's an example:
var setores = [
"Almoxarifado",
"Controladoria",
"Administrativo",
"Tecnologia de Informação"
];
var i = 0;
setores.forEach(function(item){
i++;
addOption(i, item);
});
function addOption(i, valor) {
var option = new Option(valor, i );
var select = document.getElementById("cmb-setor");
select.add(option);
}
$('#cmb-setor').trigger("chosen:updated");
$('.btn-save').on('click', function(){
var id = $('#cmb-setor').val();
console.log("Setor: "+id);
if( id == 0 ){
$('select[id="cmb-setor"]').css("border-color","red");
// $('select[id="cmb-setor"]').css({"border-color":"red", "display":"block"});
}
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.css" rel="stylesheet"/>
<div class="form-group">
<select class=" form-control" id="cmb-setor" data-placeholder="Escolha um setor">
<option value="0"></option>
</select>
</div>
<button class="btn btn-primary btn-save">Salvar</button>
<script>
$('#cmb-setor').chosen( {allow_single_deselect: true} );
</script>