Elements

Element APIs allow you to create and manage Elements in the Designer. In this overview we’ll review the types of Elements you can insert onto the canvas, the methods that are particular to the properties or type of Element you’re working with, and the best practices for working with Elements.

Creating Elements

In order to add an Element into the Designer Canvas, you need to first select an Element Preset. Once selected, you can place a new Element on the canvas by either inserting it into a specific place in the existing Element Hierarchy, or by nesting elements inside of one another to create parent-child relationships.

Selecting an Element Preset

The Element Presets object specifies all the element types available in the Designer. Use the Element Presets object to select the Element type you’d like to insert onto a page.

With webflow.elementPresets.<preset>, any Element Preset can be passed as a parameter to the Element creation methods as shown in the example below.

1// Get Selected Element
2const selectedElement = await webflow.getSelectedElement()
3
4if (selectedElement) {
5
6 // Insert DIV before selected Element
7 const newDiv = await selectedElement.before(webflow.elementPresets.DivBlock)
8
9 // Print element details
10 console.log(`${JSON.stringify(newDiv)}`)
11
12}

Once an element is created on the canvas, it can be manipulated through its respective methods, which we’ll cover in the sections below. Read through the full list of Element Presets to see all the Elements you can add to the canvas via the Designer API.

Adding Elements to the Canvas

Once you’ve identified the Element Preset you’d like to use, you can add it to the canvas via the Element creation methods. These methods allow you to place the Element on the canvas within a specific order of the Element Hierarchy, or nest elements inside of one another to create parent-child relationships.

Inserting an element

When inserting an element on the canvas, it’s important to consider the Element Hierarchy. To achieve the desired styling and flow, you need to understand where in the hierarchy your new element belongs. To insert an Element into the existing Element Hierarchy:

  1. Select an element using webflow.getSelectedElement()
  2. Decide where your new Element will be positioned within the existing Element hierarchy by using:
    • before()- this method will insert your new element before the selected element
    • after() - this method will insert your new element after the selected element

Note: If the selected element has a parent element, the new element created using before() or after() will also be a child element of the same parent.

1// Get Selected Element
2const selectedElement = await webflow.getSelectedElement()
3
4if (selectedElement) {
5
6 // Insert DIV before selected Element
7 const newDiv = await selectedElement.after(webflow.elementPresets.DivBlock)
8
9 // Print element details
10 console.log(`${JSON.stringify(newDiv)}`)

Nesting a child element

Additionally, you can decide whether to establish parent-child relationships by nesting your element within another, which can greatly influence the layout and functionality of your design. In order to nest one element within another to create a parent-child relationship:

  1. Check that the element you’ve selected supports child elements, then
  2. Decide how the new Element will be nested within the selected element by using:
    1. prepend() - this method will insert your new Element as the first child element
    2. append() - this method will insert your new Element as the last child element
1// Get Selected Element
2const el = await webflow.getSelectedElement();
3
4// Check if element supports child elements
5if (el?.children) {
6
7 // Append newElement as a child to of the selected element
8 const newElement = await el?.append(webflow.elementPresets.DivBlock)
9
10 // Print element Details
11 console.log(JSON.stringify(newElement))
12
13}

Element Properties & Methods

Once an element is added or selected, a suite of Element methods are available to help you incorporate content, styles, and HTML attributes into your design. Elements have two distinct types of methods: property methods and element-specific methods.

Property Methods

An element’s properties define how you can manipulate the element on the canvas. Each type of element shares common properties that inform their respective methods. For more detailed information about specific methods for each property, refer to the ‘Element Properties & Methods’ section of the documentation.

Element Properties
PropertyDescription
configurableIndicates if the element is configurable.
childrenIndicates if the element can have child elements.
customAttributesIndicates if the element can have custom HTML attributes.
domIdIndicates whether the ID of the element can be set.
stylesIndicates if the element can have styles.
textContentIndicates whether the element can have text content.

Element-Specific Methods

Certain elements also possess methods unique to their type. You can identify an element’s type through the element.type property. These types inherit the methods of their properties but also add additional functionalities.

For instance, a String Element has an element.textContent property set to false, meaning you cannot use the element.setTextContent() method on it. However, the String Element does offer a unique method, element.setText(), which lets you modify that element’s content. It’s crucial to always consider an element’s property and element methods to understand the available functionalities.

Best Practices for Working with Elements

Type Checking

Ensure you’re working with the correct element type to apply appropriate methods effectively. Confirm the element’s type before performing operations to prevent errors and achieve desired outcomes.

1// Example: Type checking in Webflow API
2
3// Suppose you have a reference to an element
4let element = webflow.getSelectedElement();
5
6// Check the type of the element
7if (element.type === 'String') {
8 // It's safe to use methods specific to StringElement
9 let text = element.getText();
10 console.log('Text content:', text);
11} else if (element.type === 'ImageElement') {
12 // Use methods specific to ImageElement
13 let imageUrl = element.getImageSrc();
14 console.log('Image URL:', imageUrl);
15} else {
16 // Handle other types or default case
17 console.log('Element type is not supported in this context.');
18}