Set variable value

variable.set(value, options?)

Set the value of the selected variable.

Syntax

1variable.set(
2 value: string | number | SizeValue | Variable | CustomValue,
3 options?: {
4 mode: VariableMode,
5 }
6): Promise<null>;

Parameters

ParameterDescriptionType
valueThe value to set for the variable. Must match the variable’s type. You can also pass a Variable object to create an alias, a CustomValue for custom values, or null to reset a mode-specific value to its default.string | number | SizeValue | Variable | CustomValue | null
optionsAn optional parameter to set the mode value of the variable.{mode: VariableMode}
Resetting a mode-specific value

To reset a mode-specific value back to the default base value, pass null when calling variable.set() along with the mode you want to reset. Variables without a mode-specific value won’t be affected.


Variable values by type

1collection.createColorVariable(name: string, value: string | ColorVariable | CustomValue): Promise<ColorVariable>
Accepted Formats for ValueExamples
Color namecollection.createColorVariable("primary", "red");
RGB Hexcollection.createColorVariable("primary", "#ffcc11");
RGBA Hexcollection.createColorVariable("primary", "#fffcc11");

Returns

Promise<null>

A Promise that resolves to null

Examples

1// Get Collection
2const collection = await webflow.getDefaultVariableCollection();
3
4// Get Variable
5const variable = await collection?.getVariableByName("MyColorVariable");
6
7// Check Variable type and set color
8if (variable?.type === "Color") await variable.set("#fffcc11");

Designer ability

Designer AbilityLocaleBranchWorkflowSitemode
canModifyVariablesAnyMainCanvasDesign