I need to create a select
with the days of the week, and load them when they are already in the database, so I get in my index the id
of the day of the week and I have to leave it as selected
, I'm doing this, however it gets duplicated.
$("body").append(diasHorario(2)); // seleciona Terça
function diasHorario(index) {
var dias = Array('Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado', 'Domingo');
var result = "<select id='diasHorario' class='form-control'>" +
"<option value=" + index + "> " + dias[index - 1] + "</option>" +
"<option value='1'>Segunda</option>" +
"<option value='2'>Terça</option>" +
"<option value='3'>Quarta</option>" +
"<option value='4'>Quinta</option>" +
"<option value='5'>Sexta</option>" +
"<option value='6'>Sábado</option>" +
"<option value='7'>Domingo</option>" +
"</select>";
return result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>