CodeAPI

Repeater

A repeating layout.

Repeaters provide a way for you to add repeating content to a page. Repeaters consist of repeating items, each with the same layout but different data.

For example, the repeater below contains three items, each with the same layout. There is an image on the left and two text elements on the right. However, the data in each item is different. That is, the actual images and text values are different in each repeated item.

The data displayed in a repeater comes from either:

  • Connecting the repeater and the elements contained within its items to a dataset in the Editor.
  • Using the data property in conjunction with the forEachItem(), forItems(), and onItemReady() functions in code.

Repeated Item Template

Each repeater has an item template that contains the elements and initial data that are used when new items are created. The template's initial state is the state of the first repeated item that appears in the Editor. Using code, you can set the properties of, get the properties of, or call functions on the elements of the item template by selecting the elements using $w(), the global scope selector function.

Selector Scope

Selector functions are used to select specific page elements so you can work with them in code. Depending on which selector you use, you are able to select elements from the different scopes described below.

There are two types of selector functions:

Global Scope

The $w() function that is available to your Page and Site code selects elements in the global scope.

A selector with global scope can be used to select any element that is not contained in a repeater. You can also use it to select an element that is contained in a repeater, but you need to understand what that selection means.

When you select an element contained in a repeater from the global scope and you get the value of one of the element's properties, you receive the value of that element's property from the repeater's item template.

For example, here templateText is the text value of the myRepeatedText element from the repeater's item template.

 $w.onReady( function () {
   let templateText = $w("#myRepeatedText").text;
 } );

When you select an element contained in a repeater from the global scope and you set the value of one of the element's properties or call one of the element's functions, the value is set or the function is called on the repeater's item template and all repeated instances of that element.

For example, here the item template is changed so that "New Text" is the text value of the myRepeatedText element. Also, all existing repeated items have the text value of their myRepeatedText element set to "New Text".

 $w.onReady( function () {
   $w("#myRepeatedText").text = "New Text";
 } );

And here the item template is changed so that the myRepeatedImage element is hidden. Also, all existing repeated items have their myRepeatedImage element hidden.

 $w.onReady( function () {
   $w("#myRepeatedImage").hide();
 } );

Repeated Item Scope

There are two instances where you get a repeated-item-scope selector:

  • The $item parameter of the forEachItem(), forItems(), and onItemReady() event handlers.
  • When calling the $w.at() and passing it an event whose context is "COMPONENT_SCOPE". This is usually done in an event handler that handles event on an element inside a repeater.

A selector with repeated item scope can be used to select a specific instance of a repeating element.

For example, here when the myRepeatedImage element is clicked, the value of a text element in the same repeated item where the image was clicked is changed to "Selected". All the other text elements elements with the ID myRepeatedText in the other items of the repeater are not affected.

 $w.onReady( function () {
   $w("#myRepeatedImage").onClick( (event) => {
     let $item = $w.at(event.context);
     $item("#myRepeatedText").text = "Selected";
   } );
 } );

And here, when each item is ready, the value of a text element is set to a value found in that specific item's data.

 $w("#myRepeater").onItemReady( ($item, itemData, index) => {
   $item("#myRepeatedText").text = itemData.textField;
 } );

You can also use a selector with repeated item scoope to select non-repeating elements from the global scope. However, you cannot change a repeater's item template using a selector with repeated item scope.

You can restrict a selector with repeated item scope to only select elements from the current repeated items and their descendants, but not elements from the global scope by adding .scoped() to the selector so the function call looks like $item.scoped("#idToSelect").

Retrieve Repeater Item Data When Clicked

Each repeated item in a repeater has a Container element that holds all of its repeated elements. To retrieve the data associated with a specific repeated item when it is clicked, create an onClick event handler for the item's Container. Depending on how you populated the repeater with data, you either user the connected dataset or the repeater's data array to retrieve the clicked item's data in the event handler.

For a repeater populated by connecting it to a dataset:

 $w.onReady( function () {
   $w("#repeatedContainer").onClick( (event) => {
     let $item = $w.at(event.context);
     let clickedItemData = $item("#myDataset").getCurrentItem();
   } );
 } );

For a repeater populated by setting its data property:

 $w.onReady( function () {
   $w("#repeatedContainer").onClick( (event) => {
     const data = $w("#myRepeater").data;
     let clickedItemData = data.filter(item => item._id === event.context.itemId);
   } );
 } );

Table of Contents

PROPERTIES

?
Store values associated with an object.
collapsedIndicates if the element is collapsed or expanded.
dataSets or gets the repeater data.
globalIndicates if an element appears on all pages or only on the current page.
hiddenIndicates if the element is visible or hidden.
idGets the elements's ID.
isVisibleIndicates if the element is actually visible.
parentGets the element's parent element.
renderedIndicates if an element is currently displayed.
typeGets the element's type.

FUNCTIONS

?
Perform actions on an object.
collapse( )Collapses the element and sets its collapsed property to true.
expand( )Expands the element and sets its collapsed property to false.
forEachItem( )Runs a function for each repeated item.
forItems( )Runs a function for each repeated item with the given IDs.
hide( )

Hides the element and sets its hidden property to true, using an effect if specified.

onItemReady( )Sets the function that runs when a new repeated item is created.
onItemRemoved( )Sets the function that runs when a repeated item is removed.
onMouseIn( )

Adds an event handler that runs when the mouse pointer is moved onto the element.

onMouseOut( )

Adds an event handler that runs when the mouse pointer is moved off of the element.

onViewportEnter( )

Adds an event handler that runs when an element is displayed in the viewable part of the current window.

onViewportLeave( )

Adds an event handler that runs when an element is no longer displayed in the viewable part of the current window.

scrollTo( )Scrolls the page to the element using an animation.
show( )

Shows the element and sets its hidden property to false, using an effect if specified.

MIXES IN

?
Where some functionality is inherited from.
$w.Element, $w.HiddenCollapsedMixin

collapsed

Indicates if the element is collapsed or expanded.

Description

If collapsed is true, the element is not displayed on the page under any circumstances. A collapsed element, unlike a hidden element, does not take up any space on the page. When collapsed, elements positioned within 70 pixels below the collapsed element and each other move up to take the collapsed element's place where possible. The elements that move up maintain their positions relative to one another.

If collapsed is false, the element may be displayed on the page. Elements that moved up to take the collapsed element's place on the page are moved back down.

However, an expanded element (an element whose collapsed property is false) is still not displayed if:

Even if the element is not be displayed due to the conditions mentioned above, if its collapsed property is false, it's displayed when the conditions no longer apply.

To set the collapsed property on an element, use the element's collapse() and expand() functions.

If you select Collapsed on load in the element's Properties panel in the Editor, the collapsed property is set to true when the page loads.

See Also

collapse( ), expand( ), hide( ), show( ), hidden

Syntax

get collapsed(): boolean
TYPE
?
The kind of data the property stores.
boolean
DEFAULT VALUE
?
The value of a property before you explicitly set it.
false

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Get an element's collapsed status

let isCollapsed = $w("#myElement").collapsed; // false

Toggle an element's collapsed state

if( $w("#myElement").collapsed ) {
  $w("#myElement").expand();
}
else {
  $w("#myElement").collapse();
}

data

Sets or gets the repeater data.

Description

A repeater's data is stored as an array of objects. Each object in the array must contain a unique _id property which is used to match the object's data to the individual repeated items of the repeater as described below. The value of the _id property can only contain alphanumeric characters and hyphens (-). Other than _id, the objects in the repeater's data array can contain anything you want.

For example, a simple array of repeater data may look like this:

 [
   {
     "_id": "1",
     "firstName": "John",
     "lastName": "Doe",
     "image": "http://someImageUrl/john.jpg"
   },
   {
     "_id": "2",
     "firstName": "Jane",
     "lastName": "Doe",
     "image": "http://someImageUrl/jane.jpg"
   }
 ]

Repeater data is not automatically applied to the elements in the reapeated items. You choose how to use the repeater's data in the onItemReady(), onItemRemoved(), forItems(), and forEachItem() callback functions. Most often, you apply the data of a repeated item to the properties and functions of the repeated elements contained in that repeated item.

You cannot modify the data array in-place. To add, change, or remove objects from the repeater's data array:

  1. Store the value of the data property in a variable.
  2. Make changes to the objects of the shouldTriggerOnItemRemoved array.
  3. Reset the data property with the modified array.

When the repeater's data property is set:

  1. New repeated items are created for each object that has an _id value that is not already present in the current array of data objects. The elements in the new items are first populated with the data of the repeater's item template. Then the onItemReady() event handler is triggered for each of the new items. Usually, you overwrite some or all of the data populated from the item template in the onItemReady() event handler with the data for that specific item. When all of the onItemReady() event handlers have finished running, the new items are displayed.
  2. Repeated items are removed if their IDs are no longer in the array of data objects. The onItemRemoved() event handler is triggered for each of the removed items.
  3. Nothing occurs to repeated items whose IDs were already in the array of data objects, even if other data in the object has changed. To update repeated items with the new data, use the forEachItem() or forItems() functions.

Getting the data property returns the repeater's current data. If you have not yet explicitly set the repeater's data, getting the data property returns only the IDs of the current repeated items that were set in the Editor.

Syntax

get data(): Array<Object>
set data(value: Array<Object>): void
TYPE
?
The kind of data the property stores.
Array<Object>

Examples

Get a repeater's data

let repeaterData = $w("#myRepeater").data;

Set a repeater's data

const bikeData = [
  {
		"_id":"bike1",
		"img":"wix:image://v1/6875c086ef453f5727c2b5932b3b3be4.png/Red Bike#originWidth=550&originHeight=300",
		"kind":"Red Bike"
	},
	{
		"_id":"bike2",
		"img":"wix:image://v1/703b4af24578ada6f1e11725a468096e.png/Speed Bike#originWidth=550&originHeight=300",
		"kind":"Black Bike"
	},
	{
		"_id":"bike3",
		"img":"wix:image://v1/2bb3219153c347daf475067d763be40d.png/Neon Bike#originWidth=550&originHeight=300",
		"kind":"Green Bike"
	}
];

$w("#myRepeater").data = bikeData;

Set a repeater's data from a database query

import wixData from 'wix-data';

$w.onReady(function () {
  $w("#myRepeater").onItemReady( ($item, itemData, index) => {
    $item("#bookTitle").text = itemData.title;
    $item("#bookSubtitle").text = itemData.subtitle;
    $item("#bookCover").src = itemData.pic;
  } );

  wixData.query("Books")
    .find()
    .then( (results) => {
      $w("#myRepeater").data = results.items;
    } );
} );

Modify a repeater's data

// get current data array
let dataArray = $w("#myRepeater").data;

// change something in the data array
dataArray[0].somefield = "New value";

// reset repeater data
$w("#myRepeater").data = dataArray;

Set a repeater's data

This example defines an event handler to handle the creation of new items and sets a repeater's data.

// static repeater data
const bikeData = [
  {
    "_id":"bike1",
    "img":"wix:image://v1/6875c086ef453f5727c2b5932b3b3be4.png/Red Bike#originWidth=550&originHeight=300",
    "description":"Red Bike"
  },
  {
    "_id":"bike2",
    "img":"wix:image://v1/703b4af24578ada6f1e11725a468096e.png/Speed Bike#originWidth=550&originHeight=300",
    "description":"Black Bike"
  },
  {
    "_id":"bike3",
    "img":"wix:image://v1/2bb3219153c347daf475067d763be40d.png/Neon Bike#originWidth=550&originHeight=300",
    "description":"Green Bike"
  }
];

$w.onReady(function () {
  // handle creation of new repeated items
  $w("#myRepeater").onItemReady( ($item, itemData, index) => {
    $item("#image1").src = itemData.img;
    $item("#text1").text = itemData.description;

    $w("#image1").onClick( (event) => {
      let $item = $w.at(event.context);
      $item("#text1").text = "Selected";
    } );
  } );

  // set the repeater data, triggering the creation of new items
  $w("#myRepeater").data = bikeData;
} );

Set a repeater's data and add new data on a button click

This example defines an event handler to handle the creation of new items and sets a repeater's data. It also adds an event handler to an "addButton" that is not part of the repeater. Clicking the button resets the repeater's data, adding another item.

// static repeater data part 1
const bikeData1 = [
  {
    "_id":"bike1",
    "img":"wix:image://v1/6875c086ef453f5727c2b5932b3b3be4.png/Red Bike#originWidth=550&originHeight=300",
    "description":"Red Bike"
  },
  {
    "_id":"bike2",
    "img":"wix:image://v1/703b4af24578ada6f1e11725a468096e.png/Speed Bike#originWidth=550&originHeight=300",
    "description":"Black Bike"
  }
];

// static repeater data part 2
const bikeData2 = [
  {
    "_id":"bike3",
    "img":"wix:image://v1/2bb3219153c347daf475067d763be40d.png/Neon Bike#originWidth=550&originHeight=300",
    "description":"Green Bike"
  }
];

$w.onReady(function () {
  // handle creation of new repeated items
  $w("#myRepeater").onItemReady( ($item, itemData, index) => {
    $item("#image1").src = itemData.img;
    $item("#text1").text = itemData.description;

    $w("#image1").onClick( (event) => {
      let $item = $w.at(event.context);
      $item("#text1").text = "Selected";
    } );
  } );

  // set the repeater data to be the first part of the static data,
  // triggering the creation of new items
  $w("#myRepeater").data = bikeData1;

  // add a handler for the "add" button that resets the repeater data
  // to be both parts of the static data, triggering the creation of
  // a new item
  $w("#addButton").onClick( () => {
    let tempData = $w("#myRepeater").data;
    $w("#myRepeater").data = tempData.concat(bikeData2);
  } );
} );

Set the a repeater's data and remove some data on a button click

This example defines event handlers to handle the creation of new items and the removal of existing items. Then it sets a repeater's data. Lastly, it adds an event handler to a "removeButton" that is not part of the repeater. Clicking the button resets the repeater's data, removing one of the items.

// static repeater data
const bikeData = [
  {
    "_id":"bike1",
    "img":"wix:image://v1/6875c086ef453f5727c2b5932b3b3be4.png/Red Bike#originWidth=550&originHeight=300",
    "description":"Red Bike"
  },
  {
    "_id":"bike2",
    "img":"wix:image://v1/703b4af24578ada6f1e11725a468096e.png/Speed Bike#originWidth=550&originHeight=300",
    "description":"Black Bike"
  },
  {
    "_id":"bike3",
    "img":"wix:image://v1/2bb3219153c347daf475067d763be40d.png/Neon Bike#originWidth=550&originHeight=300",
    "description":"Green Bike"
  }
];

$w.onReady(function () {
  // handle creation of new repeated items
  $w("#myRepeater").onItemReady( ($item, itemData, index) => {
    $item("#image1").src = itemData.img;
    $item("#text1").text = itemData.description;

    $w("#image1").onClick( (event) => {
      let $item = $w.at(event.context);
      $item("#text1").text = "Selected";
    } );
  } );

  // handle removal of new repeated items
  $w("#myRepeater").onItemRemoved( (itemData) => {
    console.log(`Removed: ${JSON.stringify(itemData)}`);
  } );

  // set the repeater data to be the first part of the static data,
  // triggering the creation of new items
  $w("#myRepeater").data = bikeData;

  // add a handler for the "remove" button that resets the repeater data
  // with the middle object removed, triggering the removal of the middle
  // repeated item
  $w("#removeButton").onClick( () => {
    let tempData = $w("#myRepeater").data;
    tempData.splice(1,1);
    $w("#myRepeater").data = tempData;
  } );
} );

global

Indicates if an element appears on all pages or only on the current page.

Description

If global is true, the element appears on all pages.

If global is false, the element only appears on the current page.

Syntax

get global(): boolean
TYPE
?
The kind of data the property stores.
boolean
DEFAULT VALUE
?
The value of a property before you explicitly set it.
false

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get whether an element is displayed on all pages

let isGlobal = $w("#myElement").global; // false

hidden

Indicates if the element is visible or hidden.

Description

If hidden is true, the element is not displayed on the page under any circumstances. A hidden element, unlike a collapsed element, continues to take up the same space on the page as it did when it was visible.

If hidden is false, the element may be displayed on the page.

However, an element whose hidden property is false is still not displayed if:

Even if the element is not displayed due to the conditions mentioned above, if its hidden property is set to false, it's displayed when the conditions no longer apply.

To determine if the element is actually visible, use the isVisible property.

To set the hidden property on an element, use the element's hide() or show() functions.

If you select Hidden on load in the element's Properties panel in the Editor, the hidden property is set to true when the page loads.

Note

An element's hidden property is not the same as its isVisible property. The hidden property indicates whether the element should be displayed, while isVisible indicates if it is actually displayed.

See Also

hide( ), show( ), collapse( ), expand( ), collapsed, rendered

Syntax

get hidden(): boolean
TYPE
?
The kind of data the property stores.
boolean
DEFAULT VALUE
?
The value of a property before you explicitly set it.
false

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Get an element's hidden status

let isHidden = $w("#myElement").hidden;  // false

Toggle an element's hidden state

if( $w("#myElement").hidden ) {
  $w("#myElement").show();
}
else {
  $w("#myElement").hide();
}

id

Gets the elements's ID.

Description

The ID is the element's unique identifier. It is used when selecting elements using the $w() function.

An element's id is set in the Editor using the Properties panel.

Syntax

get id(): string
TYPE
?
The kind of data the property stores.
string

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the ID

let myId = $w("#myElement").id; // "myElement"

isVisible

Indicates if the element is actually visible.

Description

If isVisible is true, the element is displayed on the page.

If isVisible is false, the element is not displayed on the page.

The value of the isVisible property is calculated based on the hidden, collapsed, and rendered properties of the element and all of its ancestors. It is true only if the conditions exist in the element's property values and the property values of its ancestors such that the element is actually displayed on the page.

Note

An element's isVisible property is not the same as its hidden property. The isVisible property indicates whether the element is actually displayed, while hidden indicates if it should be displayed.

The isVisible property of an element remains true even if another element completely covers it so that a user cannot see it.

See Also

hidden, collapsed, rendered

Syntax

get isVisible(): boolean
TYPE
?
The kind of data the property stores.
boolean
DEFAULT VALUE
?
The value of a property before you explicitly set it.
true

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Get whether an element is visible

let isVisible = $w("#myElement").isVisible;  // true

parent

Gets the element's parent element.

Description

Some elements can contain other elements. This occurs when you drag an element onto a container element. The container is the parent of all the elements it contains.

Page, Header, and Footer are top-level elements and have no parent.

See Also

children

Syntax

get parent(): Node
TYPE
?
The kind of data the property stores.
DEFAULT VALUE
?
The value of a property before you explicitly set it.
null

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the parent element and the parent's ID

let parentElement = $w("#myElement").parent;

let parentId = parentElement.id; // "page1"

rendered

Indicates if an element is currently displayed.

Description

If rendered is true, the element is in the current DOM structure and can be used.

If rendered is false the element is not in the current DOM structure.

Some reasons the element might not be in the DOM inclue:

  • It is in a slide which is not currently showing.
  • It is in a mode which is not currently active. For example, it is in the hover mode of a hover box.

See Also

collapsed, hidden, isVisible

Syntax

get rendered(): boolean
TYPE
?
The kind of data the property stores.
boolean
DEFAULT VALUE
?
The value of a property before you explicitly set it.
false

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

type

Gets the element's type.

Syntax

get type(): string
TYPE
?
The kind of data the property stores.
string

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the element's type

let myType = $w("#myElement").type; // "$w.Type"

collapse( )

Collapses the element and sets its collapsed property to true.

Description

The collapse() function returns a Promise that is resolved when the element's collapsed property has been set to true.

To learn about the behavior of a collapsed element, see the collapsed property.

You can also collapse an element when the page loads by using the Properties panel in the Editor.

See Also

expand( ), collapsed, hide( )

Syntax

function collapse(): Promise<void>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the element's collapsed property has been set to true.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Collapse an element

$w("#myElement").collapse();

Collapse an element and log a message when done

$w("#myElement").collapse()
  .then( () => {
    console.log("Done with collapse");
  } );

Toggle an element's collapsed state

if( $w("#myElement").collapsed ) {
  $w("#myElement").expand();
}
else {
  $w("#myElement").collapse();
}

expand( )

Expands the element and sets its collapsed property to false.

Description

The expand() function returns a Promise that is resolved when the element's collapsed property has been set to false.

To learn about the behavior of an expanded element, see the collapsed property.

See Also

expand( ), collapsed, show( )

Syntax

function expand(): Promise<void>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the element's collapsed property has been set to false.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Expand an element

$w("#myElement").expand();

Expand an element and log a message when done

$w("#myElement").expand()
  .then( () => {
    console.log("Done with expand");
  } );

Toggle an element's collapsed state

if( $w("#myElement").collapsed ) {
  $w("#myElement").expand();
}
else {
  $w("#myElement").collapse();
}

forEachItem( )

Runs a function for each repeated item.

Description

Use the forEachItem() function to run a function on all of a repeater's repeated items. You can use the callback function to update or pull information from all of the repeater's repeated items.

When you set a repeater's data property with data that changes items with existing IDs, those changes are not automatically reflected in the elements contained in the repeater. That is because you are responsible for applying a repeater's data to its repeated items.

To apply the data to items with new IDs, you can use the onItemReady() event handler.

To update items with existing IDs, you can use the forEachItem() or forItems() functions.

Usually, when updating repeated items you:

  • Apply the repeated item's itemData to the elements contained in each of the repeater's repeated items.
  • Add event handlers to the elements contained in each of the repeater's repeated items.

Syntax

function forEachItem(callback: ForItemCallback): void
callback ForItemCallback($item: $w, itemData: Object, index: number): void
PARAMETERS
?
The kind of data the property stores.
callback
function($item: $w, itemData: Object, index: number)
The name of the function or the function expression to run for each repeated item.
?
The kind of data the property stores.
$item
A selector function with repeated item scope.
itemData
Object
The object from the repeater's data array that corresponds to the current repeated item.
index
number
The index of the itemData object in the data array.

Examples

Loop through all of a repeater's repeated items

$w("#myRepeater").forEachItem( ($item, itemData, index) => {
  let repeatedElement = $item("#repeatedElement");
  let nonRepeatedElement = $item("#nonRepeatedElement");
  let itemDataValue = itemData.someProperty;
  let isEvenItem = index % 2 == 0;
} );

Update data in all of a repeater's repeated items

$w("#myRepeater").forEachItem( ($item, itemData, index) => {
  $item("#repeatedImage").src = itemData.img;
  $item("#repeatedText").text = itemData.description;
} );

Update data in all of a repeater's repeated items

This example uses a forEachItem() to override some of the data populated into a repeater that is connected to a dataset. Here we want a text element to contain text that changes based on the value of a certain boolean field. When a page loads, the dataset loads after the repeater. So we wait for the dataset's onReady() to call a forEachItem() that sets the desired values.

$w.onReady(function () {
    $w("#myDataset").onReady( () => {
      $w("#myRepeater").forEachItem( ($item, itemData, index) => {
          if(itemData.boolField){
              $item("myText").text = "Yes Ma'am!";
          }
          else {
              $item("#myText").text = "No way Jose!";
          }
      } );
  } );
} );

forItems( )

Runs a function for each repeated item with the given IDs.

Description

Use the forItems() function to run a function on a specified list of repeated items. You can use the callback function to update or pull information from the specified repeated items.

When you set a repeater's data property with data that changes items with existing IDs, those changes are not automatically reflected in the elements contained in the repeater. That is because you are responsible for applying a repeater's data to its repeated items.

To apply the data to items with new IDs, you can use the onItemReady() event handler.

To update items with existing IDs, you can use the forEachItem() or forItems() functions.

Usually, when updating repeated items you:

  • Apply the repeated item's itemData to the elements contained in each of the repeater's repeated items.
  • Add event handlers to the elements contained in each of the repeater's repeated items.

Syntax

function forItems(itemIds: Array<string>, callback: ForItemCallback): void
callback ForItemCallback($item: $w, itemData: Object, index: number): void
PARAMETERS
?
The kind of data the property stores.
itemIds
Array<string>
The IDs of the items on which to run the callback function.
callback
function($item: $w, itemData: Object, index: number)
The name of the function or the function expression to run for each repeated item.
?
The kind of data the property stores.
$item
A selector function with repeated item scope.
itemData
Object
The object from the repeater's data array that corresponds to the current repeated item.
index
number
The index of the itemData object in the data array.

Examples

Loop through some of a repeater's repeated items

$w("#myRepeater").forItems( ["item1", "item4"], ($item, itemData, index) => {
  let repeatedElement = $item("#repeatedElement");
  let nonRepeatedElement = $item("#nonRepeatedElement");
  let itemDataValue = itemData.someProperty;
  let isEvenItem = index % 2 == 0;
} ) ;

Update data in some of a repeater's repeated items

$w("#myRepeater").forItems( ["item1", "item4"], ($item, itemData, index) => {
  $item("#repeatedImage").src = itemData.img;
  $item("#repeatedText").text = itemData.description;
} );

hide( )

Hides the element and sets its hidden property to true, using an effect if specified.

Description

The hide() function hides the element and returns a Promise that is resolved when the effect is complete and the element's hidden property has been set to true.

To learn about the behavior of a hidden element, see the hidden property.

You can optionally apply an effect when hiding the element by providing an effectName value. You can also customize the effect by providing the optional effectOptions object.

Effects:

You can also hide an element when the page loads by using the Properties panel in the Editor.

See Also

show( ), hidden, collapse( )

Syntax

function hide([effectName: string], [effectOptions: ArcEffectOptions | BounceEffectOptions | FadeEffectOptions | FlipEffectOptions | FloatEffectOptions | FlyEffectOptions | FoldEffectOptions | GlideEffectOptions | PuffEffectOptions | RollEffectOptions | SlideEffectOptions | SpinEffectOptions | TurnEffectOptions | ZoomEffectOptions]): Promise<void>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the effect is complete and the element's hidden property has been set to true.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Hide an element with no effect

$w("#myElement").hide();

Hide an element with the "fade" effect

$w("#myElement").hide("fade");

Hide an element with an effect and log message when the effect is done

let fadeOptions = {
  "duration":   2000,
  "delay":      1000
};

$w("#myElement").hide("fade", fadeOptions);

Hide an element with an effect and log message when the effect is done

$w("#myElement").hide("fade")
  .then( ( ) => {
    console.log("Done with fade");
} );

Toggle an element's hidden state

if( $w("#myElement").hidden ) {
  $w("#myElement").show();
}
else {
  $w("#myElement").hide();
}

onItemReady( )

Sets the function that runs when a new repeated item is created.

Description

Use the onItemReady() function for code you want to run before new repeated items are rendered.

The callback is triggered when you add new items by setting the data property. It is not triggered for existing items that are updated when you set the data property. To run code after updating existing items, use the forEachItem or forItems functions.

Usually, you use onItemReady() to:

  • Apply the repeated item's itemData to the properties and functions of the repeated elements contained in the repeated item being created.
  • Add event handlers to the repeated elements contained in the repeated item being created.

Note

When using a dataset to populate the contents of your repeated items, the onItemReady() callback function is triggered before the dataset populates the values of your page elements. Therefore, element values that you set using onItemReady() may be overridden when the dataset is ready. To change the values set by the dataset, use forEachItem inside the datset's onReady(). For more information, see the forEachItem examples.

Syntax

function onItemReady(handler: ItemReadyEventHandler): Repeater
callback ItemReadyEventHandler($item: $w, itemData: Object, index: number): void
PARAMETERS
?
The kind of data the property stores.
handler
function($item: $w, itemData: Object, index: number)

The name of the function or the function expression to run when the item is ready.

?
The kind of data the property stores.
$item
A selector function with repeated item scope.
itemData
Object
The object from the repeater's data array that corresponds to the repeated item being created.
index
number
The index of the itemData object in the data array.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element on which the event is now registered.

Examples

Set up new repeated items

$w("#myRepeater").onItemReady( ($w, itemData, index) => {
  let repeatedElement = $item("#repeatedElement");
  let nonRepeatedElement = $item("#nonRepeatedElement");
  let itemDataValue = itemData.someProperty;
  let isEvenItem = index % 2 == 0;
});

Set up new repeated items

This example creates an event handler that sets up new repeated items by setting the source of a repeated image element and the text value of a repeated text element to values from the repeated item's data.

It also sets an event handler that runs when the image in a repeated item is clicked. The event handler changes the value of a repeated text element to indicate whether the repeated item is "selected". It also changes the value of a non-repeated text element which serves as a counter for how many repeated items are "selected".

$w("#myRepeater").onItemReady( ($item, itemData, index) => {
  const repeatedText = $item("#repeatedText");
  const repeatedImage = $item("#repeatedImage");
  const numSelected = $item("#nonRepeatedText");

  repeatedImage.src = itemData.img;
  repeatedText.text = itemData.description;

  repeatedImage.onClick( (event) => {
    if(repeatedText.text === "Selected"){
      repeatedText.text = itemData.description;
      numSelected.text = (Number(numSelected.text) - 1).toString();
    }
    else {
      repeatedText.text = "Selected";
      numSelected.text = (Number(numSelected.text) + 1).toString();
    }
  } );
} );

onItemRemoved( )

Sets the function that runs when a repeated item is removed.

Description

Use the onItemRemoved() function for code you want to run when repeated items are removed. The callback is triggered when you remove items by setting the value of the data property to an array in which some of the existing item IDs are no longer present.

Note

The onItemRemoved() callback function is not called when the static repeated items that were set in the Editor are removed.

Syntax

function onItemRemoved(handler: ItemRemovedEventHandler): Repeater
callback ItemRemovedEventHandler(itemData: Object): void
PARAMETERS
?
The kind of data the property stores.
handler
function(itemData: Object)

The name of the function or the function expression to run when the item is removed.

?
The kind of data the property stores.
itemData
Object
The object from the repeater's data array that corresponds to the repeated item being removed.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element on which the event is now registered.

Examples

Get data from items being removed

$w("#myRepeater").onItemRemoved( (itemData) => {
  let itemDataValue = itemData.someProperty;
});

onMouseIn( )

Adds an event handler that runs when the mouse pointer is moved onto the element.

Note

Deprecation note: The $w parameter of event handlers is being deprecated. To get a scoped selector, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context). To learn more, see here.

Syntax

function onMouseIn(handler: MouseEventHandler): Element
callback MouseEventHandler(event: MouseEvent, $w: $w): void
PARAMETERS
?
The kind of data the property stores.
handler
function(event: MouseEvent, $w: $w)

The name of the function or the function expression to run when the mouse pointer is moved onto the element.

?
The kind of data the property stores.
event
The mouse event that occurred.
$w

Deprecated: A selector function. The $w function enables event handlers to work with elements in repeaters and in the global scope.

RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element to which the event handler was added.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the mouse event info when the mouse enters an element

$w("#myElement").onMouseIn( (event) => {
  let clientX = event.clientX;  // 362
  let clientY = event.clientY;  // 244
  let offsetX = event.offsetX;  // 10
  let offsetY = event.offsetY;  // 12
  let pageX = event.pageX;      // 362
  let pageY = event.pageY;      // 376
  let screenX = event.screenX;  // 3897
  let screenY = event.screenY;  // 362
} );

onMouseOut( )

Adds an event handler that runs when the mouse pointer is moved off of the element.

Note

Deprecation note: The $w parameter of event handlers is being deprecated. To get a scoped selector, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context). To learn more, see here.

Syntax

function onMouseOut(handler: MouseEventHandler): Element
callback MouseEventHandler(event: MouseEvent, $w: $w): void
PARAMETERS
?
The kind of data the property stores.
handler
function(event: MouseEvent, $w: $w)

The name of the function or the function expression to run when the mouse pointer is moved off of the element.

?
The kind of data the property stores.
event
The mouse event that occurred.
$w

Deprecated: A selector function. The $w function enables event handlers to work with elements in repeaters and in the global scope.

RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element to which the event handler was added.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the mouse event info when the mouse exits an element

$w("#myElement").onMouseOut( (event) => {
  let clientX = event.clientX;  // 362
  let clientY = event.clientY;  // 244
  let offsetX = event.offsetX;  // 10
  let offsetY = event.offsetY;  // 12
  let pageX = event.pageX;      // 362
  let pageY = event.pageY;      // 376
  let screenX = event.screenX;  // 3897
  let screenY = event.screenY;  // 362
} );

onViewportEnter( )

Adds an event handler that runs when an element is displayed in the viewable part of the current window.

Description

An element enters the viewport when the page is scrolled to show any part of the element. An element also enters the viewport if it was hidden or collapsed and is then shown or expanded in the viewable part of the current window.

Note

Deprecation note: The $w parameter of event handlers is being deprecated. To get a scoped selector, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context). To learn more, see here.

See Also

onViewportLeave( )

Syntax

function onViewportEnter(handler: EventHandler): Element
callback EventHandler(event: Event, $w: $w): void
PARAMETERS
?
The kind of data the property stores.
handler
function(event: Event, $w: $w)

The name of the function or the function expression to run when the element enters the viewport.

?
The kind of data the property stores.
event
The event that occurred.
$w

Deprecated: A selector function. The $w function enables event handlers to work with elements in repeaters and in the global scope.

RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element on which the event is now registered.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the ID of the element that has entered the viewport

$w("#myElement").onViewportEnter( (event) => {
  let targetId = event.target.id; // "myElement"
});

onViewportLeave( )

Adds an event handler that runs when an element is no longer displayed in the viewable part of the current window.

Description

An element leaves the viewport when the page is scrolled so that the elements is completely out of view. An element also leaves the viewport if it was shown or expanded and is then hidden or collapsed from the viewable part of the current window.

Note

Deprecation note: The $w parameter of event handlers is being deprecated. To get a scoped selector, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context). To learn more, see here.

See Also

onViewportEnter( )

Syntax

function onViewportLeave(handler: EventHandler): Element
callback EventHandler(event: Event, $w: $w): void
PARAMETERS
?
The kind of data the property stores.
handler
function(event: Event, $w: $w)

The name of the function or the function expression to run when the element leaves the viewport.

?
The kind of data the property stores.
event
The event that occurred.
$w

Deprecated: A selector function. The $w function enables event handlers to work with elements in repeaters and in the global scope.

RETURN VALUE
?
Value that a function evaluates to when it is finished running.
The element on which the event is now registered.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Get the ID of the element that has entered the viewport

$w("#myElement").onViewportLeave( (event) => {
  let targetId = event.target.id; // "myElement"
});

scrollTo( )

Scrolls the page to the element using an animation.

Description

The scrollTo() function returns a Promise that is resolved when the animated scroll is complete and the element is now in view.

To scroll to a specific location on the page, see the wix-window scrollTo() function.

Calling the scrollTo() function on an element in a repeated item that is selected from the global scope causes an error.

Syntax

function scrollTo(): Promise<void>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the scroll is complete.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.Element

Examples

Scroll the page to an element

$w("#myElement").scrollTo();

Scroll the page to an element and log message when done

$w("#myElement").scrollTo()
  .then( ( ) => {
    console.log("Done with scroll");
} );

show( )

Shows the element and sets its hidden property to false, using an effect if specified.

Description

The show() function shows the element and returns a Promise that is resolved when the effect is complete and the element's hidden property has been set to false.

You can optionally apply an effect when showing the element by providing an effectName value. You can also customize the effect by providing the optional effectOptions object.

Effects:

See Also

hide( ), hidden, expand( )

Syntax

function show([effectName: string], [effectOptions: ArcEffectOptions | BounceEffectOptions | FadeEffectOptions | FlipEffectOptions | FloatEffectOptions | FlyEffectOptions | FoldEffectOptions | GlideEffectOptions | PuffEffectOptions | RollEffectOptions | SlideEffectOptions | SpinEffectOptions | TurnEffectOptions | ZoomEffectOptions]): Promise<void>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the effect is complete and the element's hidden property has been set to false.

MIXED IN FROM

?
Where this functionality is inherited from.
$w.HiddenCollapsedMixin

Examples

Show an element with no effect

$w("#myElement").show();

Show an element with the "fade" effect

$w("#myElement").show("fade");

Show an element with the "fade" effect and custom options

let fadeOptions = {
  "duration":   2000,
  "delay":      1000
};

$w("#myElement").show("fade", fadeOptions);

Show an element with an effect and log message when the effect is done

$w("#myElement").show("fade")
  .then( ( ) => {
    console.log("Done with fade");
  } );

Toggle an element's hidden state

if( $w("#myElement").hidden ) {
  $w("#myElement").show();
}
else {
  $w("#myElement").hide();
}