---
title: Virtual bindings
project: jComponent V19
version: 19.194
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/guides/virtual-bindings/
updated: 2026-11-18T13:55:44.124Z
---

# Virtual bindings

> Render detached HTML and repeated rows from a private model, outside the global data.

A virtual binding connects a piece of HTML to a private model instead of a global [path](../paths/). It suits repeated content such as table rows, list items and cards, where each item has its own data.

## Bind a piece of HTML

[VBIND()](../../reference/globals/) creates the HTML and returns a binder object. Paths inside it start with a dot and are relative to the model. A dot alone stands for the whole model. The commands are the same as in [Data binding](../data-binding/).

```html
<div id="host"></div>

<script>
	var card = VBIND('<div class="card"><h3 data-bind=".name__text"></h3><span data-bind=".age__text__.senior:value >= 65"></span></div>');

	$('#host').append(card.element);

	card.set({ name: 'Ana', age: 70 });
	console.log(card.element.find('h3').text());          // Ana
	console.log(card.element.find('span').text());        // 70
	console.log(card.element.find('span').attr('class')); // senior
</script>
```

The binder is not attached to the page by itself. Add `card.element`, a jQuery object, wherever you need it.

## Update the model

```html
<div id="host"></div>

<script>
	var card = VBIND('<div><b data-bind=".name__text"></b> <i data-bind=".__text:value.name + \' (\' + value.age + \')\'"></i></div>');

	$('#host').append(card.element);
	card.set({ name: 'Ana', age: 30 });
	console.log(card.element.text());   // Ana Ana (30)

	card.set('age', 31);
	console.log(card.get('age'));       // 31
	console.log(card.element.text());   // Ana Ana (31)

	card.get().name = 'Ben';
	card.upd('name');
	console.log(card.element.text());   // Ben Ben (31)

	console.log(card.get());            // {"name":"Ben","age":31}
</script>
```

| Member | Description |
| --- | --- |
| `element` | The HTML of the binder, as a jQuery object. |
| `set(model)` | Replaces the model and redraws all bindings. |
| `set(path, value)` | Changes one field and redraws the bindings that depend on it. |
| `get([path])` | Reads the model or one field. |
| `upd([path])` | Redraws after you changed the model directly. |
| `on(event, [selector], fn)` | Adds an event listener to the element. |
| `remove()` | Removes the element and its listeners. |

## Handle events and remove the binder

```html
<div id="host"></div>

<script>
	var card = VBIND('<div><button>Open</button></div>');

	card.on('click', 'button', function() {
		console.log('clicked');
	});

	$('#host').append(card.element);
	card.element.find('button').click();   // clicked

	card.remove();
	console.log($('#host').html());        // (empty)
</script>
```

## Render a list

[VBINDARRAY()](../../reference/globals/) repeats one template for each item of an array and appends the rows to a target element. Each row receives a `data-index` attribute. When you set a new array, existing rows are reused, extra rows are removed and only rows whose data changed are redrawn.

```html
<ul id="list"></ul>

<script>
	var rows = VBINDARRAY('<li><b data-bind=".name__text"></b> <span data-bind=".price__text__format:2"></span></li>', '#list');

	rows.set([{ name: 'Tea', price: 2 }, { name: 'Milk', price: 1.5 }]);
	console.log($('#list').text());     // Tea 2.00Milk 1.50

	rows.set(1, { name: 'Soy milk', price: 2.25 });
	console.log($('#list').text());     // Tea 2.00Soy milk 2.25

	rows.get(0).price = 3;
	rows.upd(0);
	console.log($('#list').text());     // Tea 3.00Soy milk 2.25

	rows.set([{ name: 'Water', price: 1 }]);
	console.log($('#list').text());     // Water 1.00
	console.log(rows.items.length);     // 1

	console.log($('#list li').attr('data-index'));  // 0
</script>
```

| Member | Description |
| --- | --- |
| `items` | The binders of the rows. |
| `set(array)` | Renders the array. |
| `set(index, item)` | Replaces one row. |
| `get([index])` | Reads the array or one item. |
| `upd([index])` | Redraws one row, or every row, after you changed the data directly. |
| `remove()` | Removes all rows. |

## Bind a list to a path

The `vbindarray` command connects a list to a global path. Put the row in a `<template>` or `<script type="text/html">` element inside the binding. Inside a row, `el.vbind()` returns the binder of the row, and its `index` property is the position of the row.

```html
<ul id="list">
	<ui-bind path="products" config="vbindarray">
		<template>
			<li><span data-bind=".name__text"></span> <button data-bind=".id__click:remove">Remove</button></li>
		</template>
	</ui-bind>
</ul>

<script>
	function remove(el, e, id) {
		var index = el.vbind().index;
		console.log('remove', id, 'at', index);
		products.splice(index, 1);
		UPD('products');
	}

	SET('products', [{ id: 1, name: 'Tea' }, { id: 2, name: 'Milk' }, { id: 3, name: 'Soy' }]);
</script>
```

Clicking `Remove` in the second row prints `remove 2 at 1`, and the list keeps `Tea` and `Soy`.

## Format values with row methods

A value that starts with a dot calls a method of the list object.

```html
<ul id="list"></ul>

<script>
	var rows = VBINDARRAY('<li data-bind=".price__text:.money"></li>', '#list');

	rows.money = function(value) {
		return value.format(2) + ' EUR';
	};

	rows.set([{ price: 10 }, { price: 2.5 }]);
	console.log($('#list').text());   // 10.00 EUR2.50 EUR
</script>
```
