Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
Hide div , when default option is chosen

I'm currently trying to do a selection with three options: -The first, is the default one. -the 2nd option displays inputs, -and the 3rd other inputs

My problem is: that when I choose the second or the third option, my inputs change, so that's good. But when I select the default one, my inputs that were previously selected do not disappear.

There is my code snippet:

$(document).ready(function() {
    $('#bat').change(function() {
        $($(this).children("option:selected").attr("data-hide")).hide();
        $($(this).children("option:selected").attr("data-show")).show();
     $($(this).children("option[value=1]").attr("data1")).hide();
     $($(this).children("option[value=1]").attr("data2")).hide();
    });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="bat" name="card_name" class="form-select">
    <option data1='#access' data2='#amount' value="1">--Veuillez choisir une option--</option>
    <option data-hide='#access' data-show='#amount' value="A">Fichier separes : plusieurs fichiers de une page
    </option>
    <option data-hide='#amount' data-show='#access' value="B">Fichier multipages : 1 fichier de plusieurs pages
    </option>
</select>

<div id="access">div 1</div>
<div id="amount">div 2</div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use the same method as you used before and create a div around both options:

$(document).ready(function() {
    $('#bat').change(function() {
      $('#results').show()
      $($(this).children("option:selected").attr("data-hide")).hide();
      $($(this).children("option:selected").attr("data-show")).show();
    });
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="bat" name="card_name" class="form-select">
          <option data-hide='#results' data1='#access' data2='#amount' value="1">--Veuillez choisir une option--</option>
          <option data-hide='#access' data-show='#amount' value="A">Fichier separes : plusieurs fichiers de une page
          </option>
          <option data-hide='#amount' data-show='#access' value="B">Fichier multipages : 1 fichier de plusieurs pages
          </option>
        </select>
<div id="results">
<div id="access" style="display:none;">div 1</div>
<div id="amount" style="display:none;">div 2</div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Both Hide and show occur simultaneously that's why when you select the default option value both div do not show. Just pull up below two lines of code it will work fine. I keep your HTML as it was

  $(document).ready(function() {
    $('#bat').change(function() {          
    $($(this).children("option:selected").attr("data2")).show();
    $($(this).children("option:selected").attr("data1")).show();

    $($(this).children("option:selected").attr("data-hide")).hide();
    $($(this).children("option:selected").attr("data-show")).show();
    });
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda