Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple parent and child elements served with two separate loops on the backend and I need to append the children to parent elements in sequential order. To illustrate my situation:

Parent
Parent
Parent

Child
Child
Child

What I am trying to achieve:

Parent
  - Child
Parent
  - Child
Parent
  - Child

All of the children share class
to-be-moved
and all parents share class
wine-gallery-wrap
. Unfortunately I cannot add any unique identifiers to any of the elements.

What I have tried:

This is what I tried but my JS knowledge doesn't allow me to make it work:

JavaScript
const moveItems = document.querySelectorAll('.to-be-moved');
 moveItems.forEach( function(move) {
    document.getElementsByClassName('wine-gallery-wrap').appendChild(move);
 });

Please help!
Posted
Updated 24-Dec-20 5:57am

You didn't explain why you couldn't give any sort if unique identifier to your target elements but let's assume you are correct. You have only one possible route and that is one that relies upon you getting your element in precise order at all times.

If that's the case, you can use the array returned from getElementByClassName() and sequentially append to each items as your iterate through the return list.

However, and this may sound a bit cynical, but that is not the intent:

If you cannot put any sort of unique identifier for the objects than what difference does it make which child goes to which parent? If they are in no way unique then you have defined that scenario.

If, on the other hand, you are doing this because you are creating the parent/child sets dynamically then you can create unique identifiers by serializing them at creation and keeping the parents and children synchronized.

A massive amount of the work done (using my javascript/php/html) where I currently work is all dynamically generated and uses this serialization. Most of the time it's pretty straight forward - sometimes you need a little imagination. Keeping things in precise order is key to much of it!
 
Share this answer
 
Comments
Philipp Soldunov 24-Dec-20 12:00pm    
Well the thing is that those elements are generated by Webflow's CMS and I have no access to it. So best I could do is to manipulate the output of CMS with my custom Javascript. Each parent element and each child element are related in sequential order. So first child has to go to first parent, second to second and so on. Otherwise whole functionality that I was trying to achieve would break.
Philipp Soldunov 24-Dec-20 12:02pm    
You can see the thing here - https://garconwines-master.webflow.io/. I already figured it out, you can see my solution at the bottom. Anyway, the child elements in this instance are rows of thumbnails below main image.
W Balboos, GHB 24-Dec-20 12:05pm    
One problem is using a framework - and now you exchange some ease, up front, for a loss of true flexibility and control. That's your choice.

The solution you posed is, essentially what my second paragraph says: use the returned array and index through it adding items that are correlated via the index.
Philipp Soldunov 24-Dec-20 12:08pm    
Yes Webflow has a ton of limitations, but it's my bread and butter these days. Furthermore I enjoy finding solutions to my more extreme ideas.
I think you should process the "parent" first; then retrieve the child at the same index / offset; e.g. get first parent (index 0), get child at 0; next parent (+1), etc.

It also works child first but seems less intuitive. And it's not a "multiple to multiple" as you imply; it's just single to single.
 
Share this answer
 
This bit of code helped me:

JavaScript
const moveItems = document.querySelectorAll('.to-be-moved');
let targetElements = document.querySelectorAll('.wine-gallery-wrap');
 
 for (let i = 0; i < targetElements.length; i++) {
       targetElements[i].appendChild(moveItems[i]);
 }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900