Attaching an element to the DOM

Hello, happy to be the first to post here!

Just trying out what’s possible with app development and running into my first issue.

I’m creating an element with const newDiv = webflow.createDOM("div"); then modifying it with newDiv.setAttribute("myAttribute", "myValue"); but when I save it with await newDiv.save(); I get the error “Cannot save detached element”.

How do I attach this new element to the DOM?

Figured this out. I just need to add the new element as a child to the selected element.

const parent = await webflow.getSelectedElement();
const child = webflow.createDOM("div");
child.setAttribute("myAttribute", "myValue");

parent.setChildren([child]);
await parent.save();
5 Likes