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 8026985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:40:03+00:00 2026-06-04T23:40:03+00:00

I have two div elements inside another. At times, from user interactivity, these two

  • 0

I have two div elements inside another. At times, from user interactivity, these two items are removed, and two new elements are placed.

Is it proper to remove the existing elements first? Or just overwrite the html? Or does it even matter?

$('#item1').remove();
$('#item2').remove();
$('#itemWindow').append(newItem1);
$('#itemWindow').append(newItem2);

Or simply

$('#itemWindow').html(newItem1);
$('#itemWindow').append(newItem2);

One is less code, but should the item be removed instead? The items do not have any listeners, but if they did does that make a difference?

I’m an ActionScript developer diving into JS and jQuery. In AS, if any listeners exist, it’s proper to remove the item first to sever any ties to the object, for proper memory collection. Are these rules the same with JS and jQuery?

Thanks!

  • 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-06-04T23:40:05+00:00Added an answer on June 4, 2026 at 11:40 pm

    They have no difference. So you can go ahead and use the second method. as when you use this

    $('#itemWindow').html(newItem1);
    

    $('#item1') and $('#item2') will be replaced. So you can skip removing those manually.

    As @glavić mentioned in comment if you look at the definition of html method in jQuery source here https://github.com/jquery/jquery/blob/master/src/manipulation.js#L213
    You will find out at the end it has these lines

     if ( elem ) {
        this.empty().append( value );
     }
    

    Where in this case elem is true. So the element will be emptied and then new element will be appended.

    And if they had listeners then you have to bind listeners in a way so it works with dynamically added elements like using $.on

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two HTML elements inside a div. I want to display them in
I have a <div> with two floated elements inside of it. I'm using jQuery
I have two input elements inside a div (a textbox and an image), intended
I have two HTML div elements, one source one destiny; inside of each one
I have two div elements that are twins (i.e. their dimensions and contents are
I have two div -elements, the top one has the height of 40% and
I have two DIV tags namely respond-1 and respond-2. I want to select these
The scenario is I have two div s: one is where I select items
I have one parent DIV inside parent DIV i have two DIV one in
How to wrap one div around another? I have following two div ids: #course

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.