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

# Using components

> Place components on a page, configure them, and find and control them from code.

A component is a reusable piece of UI. This guide shows how to place and control components that already exist. To write your own, read [Creating components](../creating-components/).

## Place a component

```html
<ui-component name="stat" path="sales.today" config="label:Sales today;unit:EUR"></ui-component>

<script>
	COMPONENT('stat', 'label:Value;unit:', function(self, config) {
		self.setter = function(value) {
			self.text(config.label + ': ' + value + ' ' + config.unit);
		};
	});

	SET('sales.today', 1250);
</script>
```

The element renders `Sales today: 1250 EUR`.

| Attribute | Description |
| --- | --- |
| `name` | The name of the component. |
| `path` | The [path](../paths/) of the value the component works with. |
| `config` | The configuration of this instance. |
| `default` | A JavaScript expression with the default value. |

## Write the configuration

The configuration is a list of `key:value` pairs separated by semicolons. Numbers and the words `true` and `false` become real numbers and booleans. Write `\;` and `\:` to use these characters in a value. URLs that start with `http://`, `https://`, `ws://` or `wss://` need no escaping.

```html
<ui-component name="show" config="title:Report;size:12;ratio:1.5;open:true;url:https://example.com/a;note:one\;two;time:10\:30"></ui-component>

<script>
	COMPONENT('show', function(self, config) {
		self.make = function() {
			console.log(config.title, config.size, config.ratio, config.open, config.url, config.note, config.time);
			console.log(typeof config.size, typeof config.open);
		};
	});
</script>
```

The component prints `Report 12 1.5 true https://example.com/a one;two 10:30`, and then `number boolean`.

## Use the data attribute

The `data---` attribute places a component on any element. It joins the name, path, configuration and default value with two underscores.

```html
<div id="a" data---="tag__label.text__color:red__'Default text'"></div>

<script>
	COMPONENT('tag', function(self, config) {
		self.setter = function(value) {
			self.text(config.color + ': ' + value);
		};
	});
</script>
```

Call [COMPILE()](../../reference/globals/) after you insert elements with this attribute into a running page.

## Set a default value

When the path has no value, the component stores the result of the `default` expression. Text needs quotes inside the expression. [DEFAULT()](../../reference/globals/) restores the default values of all the components below a path.

```html
<ui-component name="field" path="form.city" default="'Rome'"></ui-component>
<ui-component name="field" path="form.size" default="10"></ui-component>

<script>
	COMPONENT('field', function(self) {
		self.setter = function(value) {
			self.text(value);
		};
	});

	ON('ready', function() {
		console.log(form.city, form.size);  // Rome 10

		SET('form', { city: 'Oslo', size: 20 });
		DEFAULT('form');
		console.log(form.city, form.size);  // Rome 10
	});
</script>
```

## Special configuration keys

| Key | Effect |
| --- | --- |
| `$id:name` | Sets the identifier used by `FIND('#name')`. |
| `$class:name` | Toggles a CSS class on the element. |
| `$assign:path` | Stores the component instance at a path. |
| `$init:name` | Calls the global function `name(component)` when the component is ready. |

```html
<ui-component id="el" name="panel" config="$id:main;$class:highlight;$assign:panels.main;$init:onpanel"></ui-component>

<script>
	COMPONENT('panel', function(self) {});

	function onpanel(com) {
		console.log('init', com.id);
	}

	ON('ready', function() {
		console.log(FIND('#main').name);  // panel
		console.log(document.getElementById('el').className);
	});
</script>
```

## Link a setting to a path

A `=` in front of a key reads the setting from a path and updates the component each time that path changes.

```html
<ui-component name="heading" config="=title:page.title;level:2"></ui-component>

<script>
	COMPONENT('heading', function(self, config) {
		self.configure = function(key, value) {
			if (key === 'title')
				self.text(value);
		};
	});

	SET('page.title', 'Draft');
</script>
```

The element shows `Draft`, and it shows `Published` after `SET('page.title', 'Published')`.

## Find and control components

[FIND()](../../reference/globals/) returns a component. With `true` as the second argument it returns an array of all the matches. [SETTER()](../../reference/globals/) calls a method on all the matches, and [RECONFIGURE()](../../reference/globals/) changes their configuration.

```html
<ui-component name="alert" path="msg.a" config="$id:first"></ui-component>
<ui-component name="alert" path="msg.b"></ui-component>

<script>
	COMPONENT('alert', function(self, config) {

		self.flash = function(text) {
			console.log(self.path, 'flash', text);
		};

		self.configure = function(key, value, init) {
			if (key === 'color' && !init)
				console.log(self.path, 'color', value);
		};
	});

	ON('ready', function() {

		console.log(FIND('alert').path);          // msg.a
		console.log(FIND('alert', true).length);  // 2
		console.log(FIND('.msg.b').path);         // msg.b
		console.log(FIND('#first').path);         // msg.a
		console.log(FIND('alert[msg.b]').path);   // msg.b

		SETTER('#first', 'flash', 'one');         // msg.a flash one
		SETTER('.msg.b/flash', 'two');            // msg.b flash two
		SETTER('alert/flash', 'all');             // msg.a flash all, msg.b flash all

		RECONFIGURE('#first', 'color:red');       // msg.a color red
	});
</script>
```

| Selector | Finds components |
| --- | --- |
| `name` | With this name. |
| `.path` | With this path. |
| `#id` | With this identifier. |
| `name[path]` | With this name and path. |

## Send a command

[CMD()](../../reference/globals/) runs a named command in every component that registered it, so the caller needs no selector.

```html
<ui-component name="box" path="a"></ui-component>
<ui-component name="box" path="b"></ui-component>

<script>
	COMPONENT('box', function(self) {
		self.command('refresh', function(reason) {
			console.log(self.path, 'refresh:', reason);
		});
	});

	ON('ready', function() {
		CMD('refresh', 'manual');
		// a refresh: manual
		// b refresh: manual
	});
</script>
```

## Add components at runtime

A `<ui-component>` element added to the page starts by itself. [ADD()](../../reference/globals/) creates one from a string with the name, path and configuration.

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

<script>
	COMPONENT('tag', 'color:gray', function(self, config) {
		self.setter = function(value) {
			self.text(config.color + ' ' + value);
		};
	});

	SET('label', 'new');

	setTimeout(function() {

		ADD('tag__label__color:green', '#host');
		$('#host').append('<ui-component name="tag" path="label" config="color:blue"></ui-component>');

	}, 300);
</script>
```

The host element ends up with `green new` and `blue new`.

## Load a component on demand

A name with the `LAZY` prefix leaves the component uninitialized until the first `SETTER()` call for it.

```html
<ui-component name="LAZY dialog"></ui-component>

<script>
	COMPONENT('dialog', function(self) {

		self.make = function() {
			console.log('dialog created');
		};

		self.open = function(title) {
			console.log('open', title);
		};
	});

	setTimeout(function() {
		console.log(FIND('dialog') == null);   // true
		SETTER('dialog/open', 'Help');         // dialog created, open Help
	}, 500);
</script>
```

## Use several versions

A component name can carry a version after `@`. The element receives the class `jc-v` followed by the version.

```html
<ui-component id="old" name="widget@1"></ui-component>
<ui-component id="new" name="widget@2"></ui-component>

<script>
	COMPONENT('widget@1', function(self) {
		self.make = () => self.text('Version ' + self.version);
	});

	COMPONENT('widget@2', function(self) {
		self.make = () => self.text('Version ' + self.version);
	});
</script>
```

## Style the state of a component

The library keeps these classes on the element of the component, so you can style each state with CSS:

| Class | Present when |
| --- | --- |
| `ui-touched` | The user interacted with the component. |
| `ui-modified` | The value was changed through the component. |
| `ui-invalid` | The validation of the component failed. |
| `ui-disabled` | The configuration contains `disabled:true`. |

```html
<ui-component id="name" name="required" path="form.name"></ui-component>

<script>
	COMPONENT('required', function(self) {

		self.make = function() {
			self.html('<input type="text" data-jc-bind="" />');
		};

		self.validate = function(value) {
			return !!value;
		};
	});
</script>
```

An empty field starts with `ui-invalid`. After the user types, it has `ui-modified` and `ui-touched`. `RESET('form')` clears them again.

## React to creation and removal

`ON('@name', fn)` runs when a component with that name is created, and `ON('#id', fn)` does the same for an identifier. `element.ui` and `$(element).component()` return the instance of an element. [FREE()](../../reference/globals/) makes the library clean up removed elements right away, which calls their `destroy` function.

```html
<ui-component id="a" name="temp"></ui-component>

<script>
	COMPONENT('temp', function(self) {
		self.destroy = function() {
			console.log('destroyed');
		};
	});

	ON('@temp', com => console.log('created', com.name));

	ON('ready', function() {
		console.log(document.getElementById('a').ui.name);   // temp
		console.log($('#a').component().name);               // temp

		$('#a').remove();
		FREE();                                              // destroyed
	});
</script>
```

## Components that are not on the page

When a page uses a component that is not declared, the library downloads it from the address in `DEF.fallback`, by default `https://cdn.componentator.com/j-{0}.html`, where `{0}` is the lowercase name of the component. Set `DEF.fallback` to an empty string to turn this off.
