0
<div class="lv1" data-id="ae35e67d" data-parent-id="">
1. Title1

    <div class="lv2" data-id="bd01cc50" data-parent-id="ae35e67d">1.3. Title1.1</div>
    <div class="lv2" data-id="7f66a92d" data-parent-id="ae35e67d">1.4. Title1.2</div>
</div>

How do I append the data below to above

<div class="lv2" data-id="e7c2dbcc" data-parent-id="ae35e67d">1.1. Title1.3</div>
<div class="lv2" data-id="cc784c42" data-parent-id="ae35e67d">1.2. Title1.4</div>

The result will be like

1. Title1
1.1. Title1.1
1.2. Title1.2
1.3. Title1.3
1.4. Title1.4

As you can see the data all have "data-id" and "lv2 parent- id" will be lv1's data-id.

Dreams
  • 8,288
  • 10
  • 45
  • 71
  • Do you have a parent div around the `lv1` and `lv2` divs? – Loyalar Aug 11 '15 at 10:17
  • 1
    I think you want to **append** (to the end) not **prepend** to the beginning – Jaromanda X Aug 11 '15 at 10:18
  • Sorry , I correct my question. I do need prepend. – Dreams Aug 11 '15 at 10:19
  • @Loyalar Yes, the lv1 will be parent node of lv2. – Dreams Aug 11 '15 at 10:20
  • How do you store the `Title 1.3` and `Title 1.4` objects? Are they in another div, are they stored as a javascript variable? – Loyalar Aug 11 '15 at 10:20
  • Yes, the it all comes from a son data array.http://stackoverflow.com/questions/31936405/how-to-sort-the-json-data-in-array-when-data-is-not-in-ordered?noredirect=1#comment51787856_31936405 – Dreams Aug 11 '15 at 10:23
  • So you have the `data` variable with the elements in them as JSON objects. Have you made any javascript logic that turns these JSON objects into HTML elements? Or is that a part of this question aswell? – Loyalar Aug 11 '15 at 10:28
  • yes,that one is par of this question also,i just ask in another way here.they have same problem – Dreams Aug 11 '15 at 10:31

1 Answers1

0
//use this to append your div to above structure
//this will append third div 
$('[data-id="7f66a92d"]').after('<div class="lv2" data-id="e7c2dbcc" data-parent-id="ae35e67d">1.1. Title1.3</div>')

//this will append fourth div 
$('[data-id="e7c2dbcc"]').after('<div class="lv2" data-id="e7c2dbcc" data-parent-id="ae35e67d">1.1. Title1.3</div>')
Sameer
  • 705
  • 10
  • 25