---
title: Component
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/component/
updated: 2026-11-18T13:58:19.462Z
---

# Component

> The properties, the methods and the delegates of a component instance.

A component instance is the first argument of the function given to `COMPONENT()`. It is created for each [ui-component](../ui-component/) element. The [Creating components](../../guides/creating-components/) guide shows how the parts work together.

## API

### id

Type: `property`

The identifier of the instance.

Returns: `String` The identifier of the instance.

The value is generated, and the `$id` config key replaces it.

### ID

Type: `property`

The generated identifier of the instance.

Returns: `String` The generated identifier of the instance.

The `$id` config key does not change it.

### name

Type: `property`

The name of the component.

Returns: `String` The name of the component.

### path

Type: `property`

The path of the component, or `null` when the element has none.

Returns: `Path` The path of the component, or `null` when the element has none.

See [Path](../path/).

### config

Type: `property`

The current configuration.

Returns: `Object` The current configuration.

Change it with `reconfigure()`, so that `configure` is called.

### element

Type: `property`

The `<ui-component>` element as a jQuery object.

Returns: `jQuery` The `<ui-component>` element as a jQuery object.

### dom

Type: `property`

The `<ui-component>` element.

Returns: `HTMLElement` The `<ui-component>` element.

### plugin

Type: `property`

The parent plugin, or `null`.

Returns: `Plugin` The parent plugin, or `null`.

See [Plugin](../plugin/).

### scope

Type: `property`

The object that paths are resolved against, which is `window`.

Returns: `Object` The object that paths are resolved against, which is `window`.

### cls

Type: `property`

The CSS class name of the component, for example `ui-grid`.

Returns: `String` The CSS class name of the component, for example `ui-grid`.

The class is not added to the element automatically.

### def

Type: `property`

A function that returns the value of the `default` attribute. It exists only when the attribute is used.

Returns: `Function` A function that returns the value of the `default` attribute. It exists only when the attribute is used.

### ready

Type: `property`

Is `true` once the instance starts to initialize.

Returns: `Boolean` Is `true` once the instance starts to initialize.

### init()

Type: `delegate`

Runs once per declaration, for the first instance of the component.

It runs ahead of `make`. Use it for work shared by all instances.

Example: The order of the delegates
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.init = () => console.log('init');
    self.make = () => console.log('make');
    self.configure = key => console.log('configure', key);
    self.setter = () => console.log('setter');
    self.done = () => console.log('done');
});
// logs: init
// logs: make
// logs: setter
// logs: done
</script>
```

### make()

Type: `delegate`

Runs when the instance is created.

Build the content of the element here.

### configure(key, value, init, prev)

Type: `delegate`

Runs for each config key when the configuration changes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The config key. |
| `value` | `Object` | yes |  | The new value. |
| `init` | `Boolean` | yes |  | `true` during the initialization. |
| `prev` | `Object` | yes |  | The previous value, or `null` during the initialization. |

It runs for all keys when the component initializes.

Example: React to a config key
```html
<ui-component name="demo" config="title:Hello"></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.configure = function(key, value, init, prev) {
        key === 'title' && console.log(value, !!init, prev);
    };
    self.make = () => setTimeout(() => self.reconfigure({ title: 'Bye' }), 100);
});
// logs: Hello true null
// logs: Bye false Hello
</script>
```

### setter(value, path, flags)

Type: `delegate`

Receives each change of the value at the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The current value. |
| `path` | `String` | yes |  | The path that changed. |
| `flags` | `Object` | yes |  | The flags of the change, for example `{ init: 1 }`. |

The default implementation writes the value to the first `input`, `textarea` or `select` inside the element. A checkbox is checked when the value, as text, is `true`, `1`, `on`, `yes` or `ok`. Assign your own function to render the value.

Example: Render the value
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.setter = function(value, path, flags) {
        console.log(value, path, JSON.stringify(flags));
    };
});

SET('common.value @touched', 5);
// logs: 5 common.value {"touched":1}
</script>
```

### setter2(value, path, flags)

Type: `delegate`

Receives each change of the value, including the changes written with `rewrite()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The current value. |
| `path` | `String` | yes |  | The path that changed. |
| `flags` | `Object` | yes |  | The flags of the change. |

Example: See the values that skip the setter
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
    self.setter = value => value && console.log('setter', value);
    self.setter2 = value => value && console.log('setter2', value);
});

com.rewrite(1);
// logs: setter2 1
</script>
```

### getter(value, [realtime])

Type: `delegate`

Processes a value entered by the user and writes it to the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The entered value. |
| `realtime` | `Boolean` | no | `false` | Writes the value without calling the `setter`. |

The value goes through the function set with `parser()` and is converted according to the `type` config key. With `realtime`, the value is written without calling the `setter`.

Example: Write a value from code
```js
com.getter('5');
console.log(common.value);
// logs: 5
```

### validate(value)

Type: `delegate`

Decides whether the value is valid.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value to check. |

Returns: `Boolean` The result of the validation. A string is also accepted.

Return `true` or an empty string for a valid value. A returned string is stored in `config.invalid` as the message, and any other result sets `config.invalid` to `true`. The method runs after each change of the value.

Example: Require a value
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self, config) {
    window.com = self;
    self.validate = value => value ? true : 'The value is required';
});

console.log(com.config.invalid);
// logs: The value is required
SET('common.value', 'x');
console.log(com.config.invalid);
// logs: false
</script>
```

### state()

Type: `delegate`

Runs when the state of the component changes.

Read the state from `config.touched`, `config.modified`, `config.invalid` and `config.disabled`.

Example: Follow the state
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self, config) {
    window.com = self;
    self.state = () => console.log(!!config.touched);
});

com.touch();
// logs: true
</script>
```

### state2(component)

Type: `delegate`

Runs when another component with the same path reports its state.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `component` | `Component` | yes |  | The component that reports the state. |

The report is made with `validate2()` or with the `notify` flag of `bind()`.

Example: Listen to a sibling
```html
<ui-component name="a" path="common.value"></ui-component>
<ui-component name="b" path="common.value"></ui-component>

<script>
COMPONENT('a', function(self) {
    window.com = self;
});

COMPONENT('b', function(self) {
    self.state2 = component => console.log(component.name);
});

com.validate2();
// logs: a
</script>
```

### done()

Type: `delegate`

Runs at the end of the initialization, when the value is set.

### destroy()

Type: `delegate`

Runs when the instance is removed.

It is called by `remove()`, and by the regular cleanup for elements that left the page. Release timers and global handlers here.

Example: Clean up
```html
<ui-component name="demo"></ui-component>

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

com.remove();
// logs: destroyed
</script>
```

### get()

Type: `method`

Reads the value at the path of the component.

Returns: `Object` The value, or `null` without a path.

Example: Read the value
```js
SET('common.value', 1);
console.log(com.get());
// logs: 1
```

### set(value, [flags])

Type: `method`

Writes a value to the path and notifies everything bound to it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value to write. |
| `flags` | `String` | no |  | Flags such as `@touched @modified`. |

Example: Write the value
```js
com.set(5, '@touched');
console.log(common.value);
// logs: 5
```

### rewrite(value, [flags])

Type: `method`

Writes a value to the path without calling the `setter` of this instance.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value to write. |
| `flags` | `String` | no |  | Flags such as `@touched @modified`. |

Other components and watchers are notified.

Example: Write without rendering
```js
com.rewrite(7);
console.log(com.get());
// logs: 7
```

### modify(value, [flags])

Type: `method`

Writes a value and marks the component as touched and modified.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value to write. |
| `flags` | `String` | no |  | Flags such as `@touched @modified`. |

Example: Write a value entered by the user
```js
com.modify('x');
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
```

### push(value)

Type: `method`

Adds a value to the array at the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value to write. |

An empty path is initialized with a new array and a single value is wrapped in an array.

Example: Add an item
```js
com.push('a');
com.push('b');
console.log(common.value.join());
// logs: a,b
```

### upd([flags])

Type: `method`

Notifies everything bound to the path, without changing the value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `flags` | `String` | no |  | Flags such as `@touched @modified`. |

The method is also available as `update()`.

Example: Announce a change made directly
```js
common.value = 5;
com.upd();
console.log(com.get());
// logs: 5
```

### refresh()

Type: `method`

Calls the `setter` with the current value.

Example: Render again
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
    self.setter = (value, path, flags) => flags.refresh && console.log('refresh');
});

com.refresh();
// logs: refresh
</script>
```

### setPath(path)

Type: `method`

Changes the path of the component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The new path. |

A `?` is replaced with the path of the parent plugin. The `setter` is not called until the next change, so call `refresh()` to render the new value.

Example: Point the component to another value
```js
com.setPath('common.other');
console.log(com.path.toString());
// logs: common.other
```

### makepath(path)

Type: `method`

Replaces the `?` in a path with the path of the parent plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |

Returns: `String` The resulting path.

Example: Resolve a relative path
```html
<ui-plugin path="orders"><ui-component name="demo"></ui-component></ui-plugin>

<script>
COMPONENT('demo', function(self) {
    self.make = () => console.log(self.makepath('?.items'));
});
// logs: orders.items
</script>
```

### bind(flags, [value])

Type: `method`

Changes the state of the component and optionally writes a value without calling the `setter`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `flags` | `String` | yes |  | The flags. |
| `value` | `Object` | no |  | The value to write. |

The flags are separated with a space, a comma or `|`, with or without `@`:

| Flag | Effect |
| --- | --- |
| `touched`, `touch` | Marks the component as touched. |
| `modified`, `changed`, `modify`, `change` | Marks the component as modified. |
| `reset` | Clears the touched and modified state. |
| `validate` | Runs the validation. |
| `notify` | Calls `state2` of the other components with the same path. |
| `setter` | Calls the `setter`. |

Example: Mark a value as entered by the user
```js
com.bind('@touched @modified', 'x');
console.log(com.get(), com.element.attr('class'));
// logs: x ui-touched ui-modified
```

### touch()

Type: `method`

Marks the component as touched and runs the validation.

Example: Mark as touched
```js
com.touch();
console.log(com.config.touched);
// logs: 1
```

### change()

Type: `method`

Marks the component as touched and modified and runs the validation.

Example: Mark as changed
```js
com.change();
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
```

### reset([noemit])

Type: `method`

Clears the touched and modified state.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `noemit` | `Boolean` | no | `false` | Skips the validation. |

The validation runs unless `noemit` is `true`.

Example: Clear the state
```js
com.change();
com.reset();
console.log(com.config.touched, com.config.modified);
// logs: 0 0
```

### isInvalid()

Type: `method`

Tells whether the component is touched and invalid.

Returns: `Boolean` A truthy value when the component is touched and invalid.

Example: Show an error only after the user was here
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
    self.validate = value => !!value;
});

console.log(!!com.isInvalid());
// logs: false
com.touch();
console.log(!!com.isInvalid());
// logs: true
</script>
```

### validate2([value])

Type: `method`

Runs the validation and calls `state2` of the other components with the same path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | no |  | The value to validate. The default is the current value. |

### reconfigure(value)

Type: `method`

Changes the configuration and calls `configure` for each key.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The keys to change. |

The value is an object or a string in the form `key:value;key:value`. It accepts the special keys of the [ui-component](../ui-component/) element, such as `$class` and `=key`.

Example: Change the configuration
```js
com.reconfigure('title:Orders;limit:5');
console.log(com.config.title, com.config.limit);
// logs: Orders 5
```

### watchconfig(key, path)

Type: `method`

Keeps a config key equal to the value at a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The config key, or `null`. |
| `path` | `String` | yes |  | The path to observe. |

With `null` as the key, the object at the path is applied as the configuration.

Example: Follow a value
```js
com.watchconfig('limit', 'common.limit');
SET('common.limit', 10);
console.log(com.config.limit);
// logs: 10
```

### formatter(fn)

Type: `method`

Sets a function that converts the value for display, or converts a value with it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | A function `(path, value, type, format)` that returns the value to display, or the value to convert. |

Returns: `Object` The converted value, when a value is given.

With a function as the argument, the default `setter` uses it from then on. With any other value, the method returns the converted value.

Example: Format a price
```html
<ui-component name="demo" path="common.value"><input /></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.formatter((path, value) => value ? '$' + value : '');
});

SET('common.value', 7);
console.log($('input').val());
// logs: $7
</script>
```

### parser(fn)

Type: `method`

Sets a function that converts the entered value for the model, or converts a value with it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | A function `(path, value, type, format)` that returns the value to store, or the value to convert. |

Returns: `Object` The converted value, when a value is given.

With a function as the argument, `getter()` uses it from then on. With any other value, the method returns the converted value.

Example: Parse a price
```html
<ui-component name="demo" path="common.value"></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
    self.parser((path, value) => value.replace('$', ''));
});

com.getter('$9');
console.log(common.value);
// logs: 9
</script>
```

### autobind20([delay], [realtimebinding])

Type: `method`

Connects the `input`, `select` and `textarea` inside the element with the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `delay` | `Number` | no | `200` | The delay in milliseconds. |
| `realtimebinding` | `Boolean` | no | `false` | Calls the `setter` also while the user types. |

The value is written after the delay, and the touched and modified states are updated from the user events.

Example: Bind an input
```html
<ui-component name="demo" path="common.value"><input /></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.make = () => self.autobind20(50);
});

$('input').val('typed').trigger('input');
setTimeout(() => console.log(common.value), 200);
// logs: typed
</script>
```

### autofill([enabled])

Type: `method`

Writes the values that the browser fills in on its own to the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `enabled` | `Boolean` | no | `true` | `false` turns the feature off. |

A browser can fill a form with saved data, such as a name or an address, without any input event. One second after the library starts, it reads the first `input`, `textarea` or `select` of each component that calls this method, and a value found there is written to the path with the flags `@change` and `@init`. The parser of the component converts the value first.

It works with the default `setter`. Components with a `default` attribute, `select` elements and range inputs are skipped. `autofill(false)` turns it off.

Example: Keep a value filled in by the browser
```html
<ui-component name="login" path="form.email"><input type="email" /></ui-component>

<script>
COMPONENT('login', function(self) {
    self.autofill();
});

// the browser fills the input with a saved address
setTimeout(() => $('input').val('ann@example.com'), 100);

setTimeout(() => console.log(GET('form.email')), 1500);
// logs: ann@example.com
</script>
```

### readonly()

Type: `method`

Turns the component into a display without `getter`, `setter` and validation.

### blind()

Type: `method`

Excludes the component from notifications and from searches by path.

### singleton()

Type: `method`

Allows only one instance of the component.

Other `<ui-component>` elements with the same name are removed.

Example: Keep one instance
```html
<ui-component name="demo"></ui-component>
<ui-component name="demo"></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.singleton();
});

console.log($('ui-component').length);
// logs: 1
</script>
```

### watch(path, callback, [autoinit])

Type: `method`

Calls a function on each change at a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path to observe. |
| `callback` | `Function` | yes |  | A function `(value, flags, path)`. |
| `autoinit` | `Boolean` | no | `false` | Calls the function right away with the current value. |

Join several paths with `+`. With a function as the first argument, the path of the component is observed. A `?` is replaced with the path of the parent plugin.

Example: Observe another path
```js
com.watch('common.user', (value, flags, path) => console.log(value, path));
SET('common.user', 'Anna');
// logs: Anna common.user
```

### unwatch(path, [callback])

Type: `method`

Removes watchers registered with `watch()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The observed path. |
| `callback` | `Function` | no |  | The watcher to remove. |

Without `callback`, all the watchers of the path registered by this component are removed. With a function as the only argument, the watcher of the component path is removed. Join several paths with `+`. A `?` is replaced with the path of the parent plugin.

Example: Stop observing a path
```js
var log = value => console.log('changed', value);

com.watch('common.count', log);
SET('common.count', 1);
com.unwatch('common.count', log);
SET('common.count', 2);
// logs: changed 1
```

### datasource(path, callback, [init])

Type: `method`

Observes a path with data for the component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path to observe. |
| `callback` | `Function` | yes |  | A function `(value, flags, path)`. |
| `init` | `Boolean` | no | `true` | Set `false` to skip the first call. |

Returns: `Component` The instance.

The function is called with the current value when the component initializes, unless `init` is `false`, and on each change. A new call replaces the data source registered before, and `datasource(null)` removes it.

Example: Render items from a path
```html
<ui-component name="demo"></ui-component>

<script>
var common = { items: ['a'] };

COMPONENT('demo', function(self) {
    self.make = function() {
        self.datasource('common.items', value => console.log(value.join()));
    };
});

SET('common.items', ['a', 'b']);
// logs: a
// logs: a,b
</script>
```

### on(name, callback)

Type: `method`

Registers a handler of a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `callback` | `Function` | yes |  | The handler. |

Join several event names with `+`. See [Events](../events/).

Example: Handle an event
```js
var handler = value => console.log(value);
com.on('refresh', handler);
EMIT('refresh', 1);
com.off('refresh', handler);
EMIT('refresh', 2);
// logs: 1
```

### off(name, [callback])

Type: `method`

Removes a handler of a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `callback` | `Function` | no |  | The handler to remove. |

Without a callback, all handlers of the event registered on this instance are removed.

### emit(name, [a], [b], [c], [d])

Type: `method`

Emits a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

Example: Emit an event
```js
ON('saved', value => console.log(value));
com.emit('saved', 'ok');
// logs: ok
```

### command(name, callback)

Type: `method`

Registers a command of this instance.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The command name. |
| `callback` | `Function` | yes |  | The handler. |

Example: Register and run a command
```js
com.command('clear', value => console.log('clear', value));
com.CMD('clear', 1);
// logs: clear 1
```

### CMD(name, [a], [b], [c], [d], [e])

Type: `method`

Runs a command registered on this instance with `command()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The command name. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |
| `e` | `Object` | no |  | The fifth argument. |

### GET(path)

Type: `method`

Reads a value from a path. A `?` is replaced with the path of the parent plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |

Returns: `Object` The value.

Example: Work with the model of the plugin
```html
<ui-plugin path="orders"><ui-component name="demo"></ui-component></ui-plugin>

<script>
COMPONENT('demo', function(self) {
    self.make = function() {
        self.SET('?.count', 3);
        console.log(self.GET('?.count'));
    };
});
// logs: 3
</script>
```

### SET(path, value)

Type: `method`

Writes a value to a path and notifies everything bound to it. A `?` is replaced with the path of the parent plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |
| `value` | `Object` | yes |  | The value to write. |

### EXEC(name, [a], [b], [c], [d])

Type: `method`

Calls a function or a plugin method.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | For example `FUNC.save`, `?/save` or `@save`. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

A `?` is replaced with the path of the parent plugin. A name that starts with `@` calls a method of the closest parent component.

Example: Call a method of the parent component
```html
<ui-component name="parent">
    <ui-component name="child"></ui-component>
</ui-component>

<script>
COMPONENT('parent', function(self) {
    self.refreshlist = value => console.log('parent', value);
});

COMPONENT('child', function(self) {
    window.com = self;
});

com.EXEC('@refreshlist', 1);
// logs: parent 1
</script>
```

### SEEX(name, [a], [b], [c], [d])

Type: `method`

Calls a plugin method or a global function, or writes the first argument to the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The method, the function or the path. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

The form `plugin/method` calls a method of the plugin and a name of a global function calls that function. Any other name is a path that receives `a`.

Example: Write to a path
```js
com.SEEX('common.status', 'done');
console.log(common.status);
// logs: done
```

### SETTER(name, [a], [b], [c], [d])

Type: `method`

Calls a method of components in the whole page, in the form `component/method`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The target, for example `grid/refresh`. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

Example: Call a method of another component
```html
<ui-component name="grid"></ui-component>
<ui-component name="demo"></ui-component>

<script>
COMPONENT('grid', function(self) {
    self.reload = value => console.log('reload', value);
});

COMPONENT('demo', function(self) {
    window.com = self;
});

com.SETTER('grid/reload', 1);
// logs: reload 1
</script>
```

### find(selector)

Type: `method`

Finds elements inside the component.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | A selector, or an object with selectors. |

Returns: `jQuery` The found elements, or an object of them.

With an object, the method searches for each selector and returns an object with the same keys.

Example: Find elements
```html
<ui-component name="demo"><input /><b></b></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
});

console.log(com.find('input').length, com.find({ title: 'b' }).title.length);
// logs: 1 1
</script>
```

### closest(selector)

Type: `method`

Finds the closest parent that matches a selector.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | A jQuery selector. |

Returns: `jQuery` The found element.

Example: Find a parent
```js
console.log(com.closest('body')[0].tagName);
// logs: BODY
```

### parent([selector])

Type: `method`

Finds a parent element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | no |  | The selector. |

Returns: `jQuery` The found element.

Without an argument, the method returns the direct parent. These values are also supported:

| Selector | Result |
| --- | --- |
| `auto` | The closest parent with a height set in its style, or `window` |
| `window` | The `window` object as jQuery |
| `document` | The `document` as jQuery |
| `parent` followed by a number | The parent that many levels above the direct parent |
| anything else | The closest parent that matches the selector |

Example: Read the direct parent
```js
console.log(com.parent()[0].tagName);
// logs: BODY
```

### html([value])

Type: `method`

Reads or replaces the HTML of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | The new content. |

Returns: `jQuery` The element, or the current HTML without an argument.

An array is joined into one string.

Example: Set the content
```js
com.html(['<b>', 'Hello', '</b>']);
console.log(com.html());
// logs: <b>Hello</b>
```

### text([value])

Type: `method`

Reads or replaces the text of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | The new text. |

Returns: `jQuery` The element, or the current text without an argument.

An array is joined into one string.

Example: Set the text
```js
com.text('Hello');
console.log(com.text());
// logs: Hello
```

### append(value)

Type: `method`

Adds HTML to the end of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The content. |

Returns: `jQuery` The element.

An array is joined into one string.

Example: Add content
```js
com.append('<i></i>');
com.append(['<b>', '</b>']);
console.log(com.html());
// logs: <i></i><b></b>
```

### empty()

Type: `method`

Removes the content of the element.

Returns: `Component` The instance.

### attr(name, [value])

Type: `method`

Reads or sets an attribute of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The attribute. |
| `value` | `String` | no |  | The new value. |

Returns: `Object` The value when reading, the instance when setting.

Example: Attributes
```js
com.attr('title', 'Hello');
com.attrd('id', 5);
console.log(com.attr('title'), com.attrd('id'));
// logs: Hello 5
```

### attrd(name, [value])

Type: `method`

Reads or sets a `data-` attribute of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name without the `data-` prefix. |
| `value` | `String` | no |  | The new value. |

Returns: `String` The value when reading.

### css(name, [value])

Type: `method`

Reads or sets a CSS property of the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The property. |
| `value` | `String` | no |  | The new value. |

Returns: `Object` The value when reading, the instance when setting.

Example: Change a style
```js
com.css('color', 'red');
console.log(com.css('color'));
// logs: rgb(255, 0, 0)
```

### aclass(name, [timeout])

Type: `method`

Adds a CSS class to the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The class. |
| `timeout` | `Number` | no |  | A delay in milliseconds. |

Returns: `Component` The instance.

Example: Work with classes
```js
com.aclass('a b');
com.rclass('a');
com.tclass('c');
console.log(com.element.attr('class'), com.hclass('c'));
// logs: b c true
```

### rclass([name], [timeout])

Type: `method`

Removes a CSS class from the element, or all classes without an argument.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no |  | The class. |
| `timeout` | `Number` | no |  | A delay in milliseconds. |

Returns: `Component` The instance.

### rclass2(search)

Type: `method`

Removes all CSS classes that contain a text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `search` | `String` | yes |  | The text to search for. |

Returns: `Component` The instance.

Example: Remove a group of classes
```js
com.aclass('size-1 size-2 box');
com.rclass2('size-');
console.log(com.element.attr('class'));
// logs: box
```

### tclass(name, [enable])

Type: `method`

Toggles a CSS class on the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The class. |
| `enable` | `Boolean` | no |  | Forces the state of the class. |

Returns: `Component` The instance.

### hclass(name)

Type: `method`

Tells whether the element has a CSS class.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The class. |

Returns: `Boolean` `true` when the class is present.

### width()

Type: `method`

Returns the width of the element in pixels.

Returns: `Number` The width.

### height()

Type: `method`

Returns the height of the element in pixels.

Returns: `Number` The height.

### event(name, [selector], callback)

Type: `method`

Registers a DOM event handler on the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The DOM event. |
| `selector` | `String` | no |  | Limits the event to matching children. |
| `callback` | `Function` | yes |  | The handler. |

Returns: `Component` The instance.

The arguments are passed to the jQuery `.on()` method.

Example: Handle a click
```html
<ui-component name="demo"><button>Save</button></ui-component>

<script>
COMPONENT('demo', function(self) {
    self.make = function() {
        self.event('click', 'button', () => console.log('clicked'));
    };
});

$('button').trigger('click');
// logs: clicked
</script>
```

### autofocus([selector])

Type: `method`

Moves the focus to the first visible control inside the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | no |  | The elements to focus. |

Returns: `Component` The instance.

The default selector is `input,select,textarea`. When no control can take the focus yet, the method tries again every 200 milliseconds, 15 times at most. The focus is not moved on mobile devices.

Example: Focus the first input
```html
<ui-component name="demo"><input id="first" /></ui-component>

<script>
COMPONENT('demo', function(self) {
    window.com = self;
});

com.autofocus();
console.log(document.activeElement.id);
// logs: first
</script>
```

### import(url, [callback], [prepare])

Type: `method`

Downloads content and appends it to the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The address. |
| `callback` | `Function` | no |  | Runs when the content is in the page. |
| `prepare` | `Function` | no |  | A function `(html)` that returns the content to insert. |

See `IMPORT()` in [Importing content](../../guides/importing/).

Example: Import a part
```js
com.import('/parts/help.html', () => console.log(com.html()), html => html + '<hr>');
// logs: <p>Help</p><hr>
```

### replace(target, [remove])

Type: `method`

Moves the component to another element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `target` | `Element` | yes |  | The new element, as a selector, a DOM element or jQuery. |
| `remove` | `Boolean` | no | `false` | Removes the original element. |

Returns: `Component` The instance.

With `remove`, the original element is removed from the page.

Example: Move the component
```js
com.replace('#box', true);
console.log(com.dom.id, $('ui-component').length);
// logs: box 0
```

### remove()

Type: `method`

Calls `destroy` and removes the component and its element.

### icon(value)

Type: `method`

Adds the icon prefix in `DEF.iconprefix` to an icon name.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The icon name. |

Returns: `String` The CSS classes of the icon.

A value with a space already contains its prefix and stays unchanged.

Example: Build icon classes
```js
console.log(com.icon('home'));
// logs: ti ti-home
console.log(com.icon('fa fa-home'));
// logs: fa fa-home
```

### caniuse(name)

Type: `method`

Finds the first declared component in a list of names separated with a comma or `|`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The names. |

Returns: `String` The found name, or `false`.

Example: Choose an available component
```js
console.log(com.caniuse('missing,demo'));
// logs: demo
console.log(com.caniuse('missing|demo'));
// logs: demo
console.log(com.caniuse('missing'));
// logs: false
```

### parseSource(value)

Type: `method`

Parses a list in the form `id|name|icon,id|name|icon`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The list. |

Returns: `Array` Objects with `id`, `name` and `icon`.

The method is also available as `parsesource()`. When the `type` config key is `number`, the identifiers are converted to numbers.

Example: Parse a list
```js
var items = com.parseSource('1|One,2|Two|home');
console.log(JSON.stringify(items[1]));
// logs: {"id":"2","name":"Two","icon":"home"}
```
