Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
hiding second select option based on first select option

I have 2 selects. Both have the same options (taken from db). Let's say the options are:

Inventory 1
Inventory 2
Inventory 3

If I choose Inventory 1 in the first select, then in the second select it should disappear. If I choose Inventory 2 in the first select, then Inventory 1 should appear back in the second select and Inventory 2 should disappear.

This is my code so far:

$('#from-location').on('change', function() {
        from_location_value = $('#from-location option:selected').val();
        to_location_value = $('#to-location option:selected').val();

        $("#to-location option[value=" + from_location_value + "]").hide();
        
    });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this

$('#from-location').on('change', function() {
    let fromValue = $('#from-location').val();
    let options = $('#to-location option').show();
    if( $(this).val() !== '' ){
        options.siblings('option[value="' + fromValue + '"]').hide()
    }
});
$('#to-location').on('change', function() {
    let toValue = $('#to-location').val();
    let options = $('#from-location option').show();
    if( $(this).val() !== '' ){
        options.siblings('option[value="' + toValue + '"]').hide()
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="from-location">
    <option value=""></option>
    <option value="Inventory 1">Inventory 1</option>
    <option value="Inventory 2">Inventory 2</option>
    <option value="Inventory 3">Inventory 3</option>
</select>
<select id="to-location">
    <option value=""></option>
    <option value="Inventory 1">Inventory 1</option>
    <option value="Inventory 2">Inventory 2</option>
    <option value="Inventory 3">Inventory 3</option>
</select>

about 4 years ago · Juan Pablo Isaza Relatório

0

I know you have already accepted an answer, but here's a different approach without duplicating code. There's only one event handler, and the behaviour you describe works for both selects - whichever option you select from whichever select will disappear from the other select.

In fact your question does not specify you want anything to happen for changes to the 2nd select, right? So this is just a demonstration of something maybe useful :-)

$('select').on('change', function() {
    // Find the value we just chose
    let selected = $(this).val();

    // Find the select which was not just changed
    let $otherSelect = $('select').not($(this));

    // First unselect anything selected from the other select
    $otherSelect.val('');
    
    // Re-display everything from the other select
    $('option', $otherSelect).show();
    
    // Hide the currently selected option from the other select
    $('option[value="' + selected + '"]', $otherSelect).hide();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="from-location">
    <option value="">Please select</option>
    <option value="Inventory 1">Inventory 1</option>
    <option value="Inventory 2">Inventory 2</option>
    <option value="Inventory 3">Inventory 3</option>
</select>
<select id="to-location">
    <option value="">Please select</option>
    <option value="Inventory 1">Inventory 1</option>
    <option value="Inventory 2">Inventory 2</option>
    <option value="Inventory 3">Inventory 3</option>
</select>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda