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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:37:46+00:00 2026-05-22T00:37:46+00:00

I am trying to add new div elements on the fly to an already

  • 0

I am trying to add new div elements on the fly to an already existing set of elements represented by common class. This set of elements also have a jQuery .data() on it, which I use to do bookkeeping stuff.

The problem is when I try to add the new elements using “before()” the before mentioned .data() values get wiped out. But this is not case with using “after()”.

Not sure if this helps but the incoming div element share the same class with the set of existing div elements to which they are being added.

I have created a self-contained example on jsfiddle.

Just toggle the lines between .after() & .before() in the javascript code to know the difference.

I will also paste here the code for easy reference.

HTML Snippet

<div id="parent-container">

    <div  id='child1' class="child">I am a child 1 - static</div>

</div>

JavaScript Snippet

$(document).ready(function() {

    var children = $(".child");

    children.data("newData", "Data for .child elements");

    alert("before dom manipulation -> " + children.data("newData"));        

    var dynamicChildDiv = $("<div id='child2' class='child'>I am child 2 - dynamic</div>");        

    // Adding a new div "after" the first .child works fine as far as preserving the existing properties on .class
    $(".child:first").after(dynamicChildDiv);

    // Adding a new div "before" the first .child causes havoc on the stored properties on .child
    //UNCOMMENT ME AFTER COMMENTING THE ".after" method above.
    //$(".child:first").before(dynamicChildDiv);

    children = $(".child");

    alert("after dom manipulation -> " + children.data("newData"));   

});
  • 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-22T00:37:47+00:00Added an answer on May 22, 2026 at 12:37 am

    When you store some data to a selection with .data() it stores it to all elements in that selection. But when you retrieve it will only select the first one.

    Since you put a new one as the first, and you do not store the same data with it, when you try to access the data it returns nothing..

    Not sure of your requirements, but you most likely do not have to store the same data to all the .child elements. Maybe just store it to their common parent, and retrieve it from there as well..


    quoting from .data( key )

    Description: Returns value at named data store for the first element in the jQuery collection, as set by data(name, value).

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

Sidebar

Related Questions

I have this HTML: <div class=region-list id=region_North_America> <strong>North America</strong> </div> and want to add
I am trying to add a new div to the page when you click
I am trying to make jquery add new div and then make it draggable
I am trying to add this HTML also when a input field is added:
I'm trying to add a new node to an jQuery SimpleTree , but all
I'm trying add a tab to my web page that looks like this: Using
I was trying to add the XML schema to an existing EJB project. JAXB
I am trying to wrap a certain number of elements in a div .
i have a dynamic table that creates a div (new panel) for every TD
I am trying to add dynamic HTML elements from the code behind file on

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.