Element Types & Methods

Each element in Webflow has a specific type that determines its functionality and available methods. While all elements share some common properties, each element type also has specialized methods that allow you to manipulate that element’s unique characteristics.

Identifying element types

You can identify an element’s type using the element.type property:

1const element = await Webflow.getSelectedElement();
2console.log(element.type); // "DOM", "String", "Image", etc.

Element presets

To add a specific element type to the canvas, you can use the webflow.elementPresets object, which contains a set of presets for different element types available in the designer.

1const element = await webflow.elementPresets.DOM;
2await webflow.addElement(element);

Element type methods

Different element types have unique methods tailored to their functionality. Always check an element’s type before applying type-specific methods.

Best Practices for Element Type Methods

  1. Always check element type before applying type-specific methods:

    1if (element.type === "Image") {
    2 // Now it's safe to use Image methods
    3 await element.setAltText("Description");
    4}
  2. Handle multiple element types with type guards:

    1function handleElement(element) {
    2 switch(element.type) {
    3 case "String":
    4 return element.getText();
    5 case "Image":
    6 return element.getAsset();
    7 default:
    8 return null;
    9 }
    10}
  3. Combine property and type checks for maximum safety:

    1if (element.type === "DOM" && element.children) {
    2 // Safe to use DOM methods and children methods
    3 await element.setTag("div");
    4 await element.append(Webflow.elementPresets.Paragraph);
    5}
Built with