1
I need to create a select
with the days of the week, and upload them when they are already in the database, then I receive in my index the id
of the day of the week and I have to leave it as selected
, I am doing so, but the same 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>
this was exactly the way I thought I would do, only it would give more code because I didn’t know this
if inline
, detail, now the days array is unnecessary and as the return is string from to put the===
and add '1' but this is detail.– Gabriel Rodrigues
It would be interesting to use the array
var dias = Array(
and make it more DRY. Here is a suggestion: http://jsfiddle.net/2xb08pzd/6/– Sergio