Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:56:10+00:00 2026-05-27T03:56:10+00:00

I’m getting very wierd behavior when I empty a div then append another div

  • 0

I’m getting very wierd behavior when I empty a div then append another div to it using selected based on selected option. Hard to explain. When you select and option it works, but when you come back and select the same optiongroup again, it stops working completely. Please have a look at his example: http://jsbin.com/akeboz/3/

It’s probably me doing it tha wrong way again 😛 but what do you think is wrong with it?

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
<script>
$(document).ready(function(){
  $('#carSelect').change(function(){
    currentBrand = $(this).find('option:selected').data('brand');
    $('#carContainer').empty();
    $('#car_'+currentBrand).show().appendTo('#carContainer');
  });
});
</script>
</head>
<body>

  <form>

     <select id="carSelect">
          <optgroup label="Swedish Cars">
            <option data-brand='volvo' value="volvo">Volvo</option>
            <option data-brand='saab' value="saab">Saab</option>
          </optgroup>
          <optgroup label="German Cars">
            <option data-brand='mercedes' value="mercedes">Mercedes</option>
            <option data-brand='audi' value="audi">Audi</option>
          </optgroup>
      </select>

      <div id="carContainer"></div>

  </form>

  <div style="display:none;">

      <div id="car_audi">
          audi stuff
      </div>

      <div id="car_mercedes">
          mercedes stuff
      </div>

  </div>


</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:56:11+00:00Added an answer on May 27, 2026 at 3:56 am

    You’re appending, but then destroying what you appended when something else is selected, so there’s no way to get it back.

    You want to make a clone of it, and append the clone.

    $('#car_'+currentBrand).clone().show().appendTo('#carContainer');
    

    Also, the .show() isn’t needed because it is their parent that is hidden, not the elements themselves.

    Ultimately, I think it would be better to have the content nested in each car type, and put it back when you’re done with it (before grabbing the new content).

    That way you’re not constantly destroying and rebuilding DOM elements.

    Like this:

     <div style="display:none;">
    
          <div id="car_audi">
              <div>audi stuff</div>
          </div>
    
          <div id="car_mercedes">
              <div>mercedes stuff</div>
          </div>
    
     </div>
    

    Then this:

     var currentBrand; 
     var container = $('#carContainer');
    
     $('#carSelect').change(function(){
         if( container.children().length ) {
             if( currentBrand ) {
                 container.children().appendTo( '#car_'+currentBrand );
             } else {
                 container.empty();
             }
         }
         currentBrand = $(this).find('option:selected').data('brand');
         container.append($('#car_'+currentBrand).children());
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.