Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!