I have this code in PHP that repeats the same select 10 times with the same information.
for ($i=1; $i < 11; $i++)
{
$output.='
<div class="form-group">
<label>Prioridad '.$i.' de horario</label><br>
<select class="form-control selectDisable">
<option selected disabled>Elige una opción</option>
<option value="1">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Lunes</option>
<option value="2">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Martes</option>
<option value="3">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Miércoles</option>
<option value="4">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Jueves</option>
<option value="5">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Viernes</option>
<option value="6">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Sábado</option>
<option value="7">Horario: 00:00 - 00:00 Break: 00:00 - 00:00 Descanso: Domingo</option>
</select>
</div>';
}
I have this with Jquery that eliminates the selected option in the other select
$(document).on('change','.selectDisable',function(){
$(this).siblings().find('option[value="'+$(this).val()+'"]').remove();
});
The problem is that it doesn't execute it, apparently <div class="form-group">
because it doesn't seem to be finding siblings within the same DIV.
I've already asked the question here before:
I insist, if I remove the div from the form-group it does it fine, but since I can handle it with that class, I have thought of
$(document).on('change','.selectDisable',function(){
$(this).closest('.form-group').find('.selectDisable').siblings().find('option[value="'+$(this).val()+'"]').remove();
});
But it didn't work out for me, any suggestions?
There is a bug in that last code that is causing it to not work:
He
siblings()
is misplaced. You don't want the brothers of theselect
, you want the brothers of.form-group
the one that are going to contain the othersselect
. To do this, move thesiblings()
before thefind(".selectDisable")
.With that change, the code would look like this and it should work for you:
But you could still simplify the code a bit more because you really wouldn't need two
find
in a row anymore: you could join them into one to make it a bit more efficient:Here I leave you a demo with only 3
select
:another option can be