The text content of an element is automatically created as a child StringElement of that element. To retrieve the text content from an element, you’ll need to retrieve the child StringElement of your target element. Once you’ve retrieved the StringElement you can use the getText() method to get the text content of your element.

Example

1// Get Selected Element
2const selectedElement = await Webflow.getSelectedElement();
3
4if (selectedElement?.textContent && selectedElement?.children) {
5
6 // Get Child Elements
7 const children = await selectedElement.getChildren();
8
9 // Filter string elements from children
10 const strings = children.filter(child => child.type === "String");
11
12 // Initialize an array to hold text content
13 let textContent = [];
14
15 // Loop over string elements to get text
16 for (const myString of strings) {
17 if (myString.type === "String") {
18 const text = await myString.getText();
19 textContent.push(text);
20 }
21 }
22
23 // Print text
24 console.log(textContent);
25}
Built with