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

113
Views
append Element based on option selection and conditions in jQuery

I am trying to create a function where the div will show based on the select box option selection and the selected option will disappear, again if the div is hidden then the select box option will appear.

e.g in my code When I select Red Option then the div#red show();

When I click cancel('red') the div is disappeared as expected by it the select option for red is still removed, I want to reflect the same.

how I achieve the goal, help

<select id="colorselector">
   <option value="0" selected>--</option>
   <option value="red">Red</option>
   <option value="yellow">Yellow</option>
   <option value="blue">Blue</option>
</select>

<div id="red" class="colors" style="display:none; color:red"> red... 
  <span><a onclick="cancel('red')">Cancel</a></span>
</div>

<div id="yellow" class="colors" style="display:none; color:yellow"> yellow.. 
  <span><a onclick="cancel('yellow')">Cancel</a></span>
</div>

<div id="blue" class="colors" style="display:none; color:blue"> blue.. 
  <span><a onclick="cancel('')">Cancel</a></span>
</div>
$(function() {
  $('#colorselector').change(function() {
    $('.colors').hide();
    $('#' + $(this).val()).show();
    $('option:selected').remove();
  });
});

function cancel(num) {
  $("div#" + num).remove();
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your issue is that .remove() deletes the html from the page - the only way to add it back would be to re-add the option (and the colour div).

Instead, use .hide() and .show().

$(function() {
  $('#colorselector').change(function() {
    $('.colors').hide();
    $("option").show();
    $('#' + $(this).val()).show();
    $('option:selected').hide();
  });
});

function cancel(num) {
  $("div#" + num).hide();
  $('option').show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="colorselector">
   <option value="0" selected>--</option>
   <option value="red">Red</option>
   <option value="yellow">Yellow</option>
   <option value="blue">Blue</option>
</select>

<div id="red" class="colors" style="display:none; color:red"> red... 
  <span><a onclick="cancel('red')">Cancel</a></span>
</div>

<div id="yellow" class="colors" style="display:none; color:yellow"> yellow.. 
  <span><a onclick="cancel('yellow')">Cancel</a></span>
</div>

<div id="blue" class="colors" style="display:none; color:blue"> blue.. 
  <span><a onclick="cancel('blue')">Cancel</a></span>
</div>

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!