Introduction

Important: This namespace is available only in Wix Blocks widget code.

Blocks widgets can expose a widget API, letting you add properties, events, and functions to your widget. The $widget namespace provides functionality for working with your widget's API from within the widget code.

Did this help?

props


propsProps

Sets or gets the widget's properties.

The props property is an object containing all of the widget's properties.

JavaScript
$w("#text1").text = $widget.props.shoe.size; // "43"
Did this help?

fireEvent( )


Fires an event that is defined in the Widget API.

The fireEvent() function fires a widget event, which can then be handled from outside the widget (by code in a parent widget or a site page). When using the widget's API, the event is named on, for example, onAddedToCart.

Method Declaration
Copy
function fireEvent(eventName: string, data: object): void;
Method Parameters
eventNamestringRequired

The name of the event to be fired.


dataDataRequired

A data object that is passed to the event handler.

Fire a widget event
JavaScript
$w(`#button1`).onClick(() => { $widget.fireEvent(`addedToCart`, { productId: product.id, customerId: customer.id, }); });
Did this help?

onPropsChanged( )


Adds an event handler that runs when the value of a widget property is changed.

A widget receives an onPropsChanged event when one or more of its properties is changed via its API.

Method Declaration
Copy
function onPropsChanged(handler: function): any;
Method Parameters
handlerfunctionRequired

handler(oldProps: object, newProps: object): void The name of the function or the function expression to run when the value of a widget property is changed.

Returns
Return Type:any
Load updated data when a widget's property is changed
JavaScript
$widget.onPropsChanged((oldProps, newProps) => { loadProductData(newProps.productId); loadCustomerData(newProps.customerId); });
Did this help?