element.setAttribute(name, value)

Set the value of the specified HTML attribute of the DOMElement. Use this method instead of the ‘Custom Attribute’ methods.

Syntax

1element.setAttribute(name: string, value: string): Promise<null>

Parameters

  • name : string - The name of the attribute to set
  • value : string - The value of the attribute to set

Returns

Promise<null>

A promise that resolves to null

Example

1// Get Selected Element
2const selectedElement = await webflow.getSelectedElement();
3
4if (selectedElement?.children) {
5
6 // Create and append DOM Element
7 const DOMElement = await selectedElement.append(webflow.elementPresets.DOM);
8
9 if (DOMElement?.type === "DOM") {
10 // Set Tag
11 await DOMElement.setTag('img');
12
13 // Set HTML Attribute
14 await DOMElement.setAttribute('src', 'https://example.com/image.jpg');
15 await DOMElement.setAttribute('alt', 'Example image');
16 await DOMElement.setAttribute('width', '300');
17
18 // Get all attributes to verify
19 const attributes = await DOMElement.getAllAttributes();
20 console.log(attributes);
21 }
22}

Designer Ability

Designer AbilityLocaleBranchWorkflowSitemode
canDesignPrimaryMainCanvasDesign
Built with