---
title: Macro
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/macro/
updated: 2026-11-18T13:58:29.465Z
---

# Macro

> The delegates, properties and methods of a macro instance.

A macro instance is the object passed to the declaration of `MACRO()`. It adds behaviour to an element with the [`macro`](../ui-bind/#macro) command of `<ui-bind>`. [Macros](../../guides/macros/) explains the concept. In the examples, `macro` is an instance attached to a `<ui-bind>` bound to `form.name`.

## API

### self.make = function()

Type: `delegate`

Runs once after the declaration.

Example: Preparing the element
```html
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self, element, cls) {
        self.make = function() {
            element.aclass(cls);
        };
    });
</script>
```

### self.setter = function(value, path, type)

Type: `delegate`

Receives the value of the path.

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

The delegate runs on each update of the binder.

Example: Rendering the value
```html
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self) {
        self.setter = function(value) {
            self.text(value);
        };
    });
    SET('form.name', 'Peter');
</script>
```

### self.service = function(counter)

Type: `delegate`

Runs once per minute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | The number of runs. |

The delegate receives a counter that grows by one on each run.

### self.destroy = function()

Type: `delegate`

Runs when the element of the binder is removed.

Example: Releasing resources
```html
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self) {
        self.destroy = () => console.log('destroyed');  // destroyed
    });
    setTimeout(function() {
        $('ui-bind').remove();
        FREE();
    }, 500);
</script>
```

### self.name

Type: `property`

The name of the macro.

Example: Reading name
```js
console.log(macro.name);  // demo
```

### self.path

Type: `property`

The path of the binder.

Example: Reading path
```js
console.log(macro.path);  // form.name
```

### self.element

Type: `property`

The element of the binder as a jQuery object.

Example: Reading element
```js
console.log(macro.element.attr('id'));  // box
```

### self.dom

Type: `property`

The element of the binder as a DOM node.

Example: Reading dom
```js
console.log(macro.dom.id);  // box
```

### self.binder

Type: `property`

The binder that created the macro.

See the [`refresh()`](../ui-bind/#refresh) and [`exec()`](../ui-bind/#exec) methods.

Example: Reading binder
```js
console.log(macro.binder.path);  // form.name
```

### self.scope

Type: `property`

The [Scope](../scope/) of the binder.

The property is assigned after the `make` delegate and exists when the path contains `?`.

Example: Reading scope
```js
console.log(macro.scope.path);  // users
```

### self.get()

Type: `method`

Reads the value of the path.

Returns: `Object` The value.

Example: Using get()
```js
SET('form.name', 'Peter');
console.log(macro.get());  // Peter
```

### self.set(value)

Type: `method`

Writes a value to the path.

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

Example: Using set()
```js
macro.set('Peter');
console.log(GET('form.name'));  // Peter
```

### self.refresh()

Type: `method`

Runs the binder again with the current value.

Returns: `Macro` The macro.

Example: Using refresh()
```js
macro.setter = value => console.log(value);  // Peter
window.form.name = 'Peter';
macro.refresh();
```

### self.EXEC(path, [a], [b], [c], [d])

Type: `method`

Calls a function by its path.

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

Returns: `Macro` The macro.

The `?` character is replaced with the path of the scope. The `@` prefix calls a method of the parent component.

Example: Using EXEC()
```js
macro.EXEC('@ping', 5);
```

### self.SEEX(path, [a], [b], [c], [d])

Type: `method`

Calls a function or writes a value.

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

Returns: `Macro` The macro.

When the path holds no function, the first argument is written to it.

Example: Using SEEX()
```js
macro.SEEX('@title', 'Orders');
console.log(FIND('owner').title);  // Orders
```

### self.find(selector)

Type: `method`

Finds nested elements.

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

Returns: `jQuery|Object` The result.

An object of selectors returns an object of jQuery results with the same keys.

Example: Using find()
```js
console.log(macro.find('.label').text());  // Text
```

### self.closest(selector)

Type: `method`

Finds the closest matching parent.

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

Returns: `jQuery` The result.

Example: Using closest()
```js
console.log(macro.closest('body').length);  // 1
```

### self.parent([selector])

Type: `method`

Returns a parent element.

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

Returns: `jQuery` The result.

The argument accepts the same values as [`parent()`](../component/#parent) of a component.

Example: Using parent()
```js
console.log(macro.parent().prop('tagName'));  // BODY
```

### self.html([value])

Type: `method`

Reads or replaces the HTML content.

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

Returns: `String|jQuery` The content, or the element when `value` is passed.

An array is joined.

Example: Using html()
```js
macro.html('<i>A</i>');
console.log(macro.html());  // <i>A</i>
```

### self.text([value])

Type: `method`

Reads or replaces the text content.

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

Returns: `String|jQuery` The content, or the element when `value` is passed.

An array is joined.

Example: Using text()
```js
macro.text('A');
console.log(macro.text());  // A
```

### self.append(value)

Type: `method`

Adds content to the end of the element.

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

Returns: `jQuery` The element.

An array is joined.

Example: Using append()
```js
macro.append('<i>A</i>');
console.log(macro.find('i').length);  // 1
```

### self.empty()

Type: `method`

Removes the content of the element.

Returns: `jQuery` The element.

Example: Using empty()
```js
macro.empty();
console.log(macro.html() === '');  // true
```

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

Type: `method`

Attaches a jQuery event to the element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the event. |
| `selector` | `String` | no |  | The delegated selector. |
| `callback` | `Function` | yes |  | The handler. |

Returns: `Macro` The macro.

The arguments are the same as in the jQuery `on()` method.

Example: Using event()
```js
macro.event('click', '.label', () => console.log('clicked'));  // clicked
macro.find('.label').click();
```

### self.aclass(classes, [timeout])

Type: `method`

Adds CSS classes.

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

Returns: `Macro` The macro.

Example: Using aclass()
```js
macro.aclass('active');
console.log(macro.hclass('active'));  // true
```

### self.rclass([classes], [timeout])

Type: `method`

Removes CSS classes.

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

Returns: `Macro` The macro.

Without an argument, all classes are removed.

Example: Using rclass()
```js
macro.aclass('active');
macro.rclass('active');
console.log(macro.hclass('active'));  // false
```

### self.rclass2(search)

Type: `method`

Removes CSS classes that contain a text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `search` | `String|RegExp` | yes |  | The text or a regular expression. |

Returns: `Macro` The macro.

Example: Using rclass2()
```js
macro.aclass('col-1 col-2');
macro.rclass2('col-');
console.log(macro.hclass('col-2'));  // false
```

### self.tclass(classes, [state])

Type: `method`

Toggles CSS classes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `classes` | `String` | yes |  | The classes. |
| `state` | `Boolean` | no |  | Forces the state. |

Returns: `Macro` The macro.

Example: Using tclass()
```js
macro.tclass('active', true);
console.log(macro.hclass('active'));  // true
```

### self.hclass(name)

Type: `method`

Tells whether the element has a CSS class.

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

Returns: `Boolean` The result.

Example: Using hclass()
```js
console.log(macro.hclass('missing'));  // false
```

### self.attr(name, [value])

Type: `method`

Reads or writes an attribute.

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

Returns: `String|Macro` The value, or the macro when `value` is passed.

Example: Using attr()
```js
macro.attr('title', 'Orders');
console.log(macro.attr('title'));  // Orders
```

### self.attrd(name, [value])

Type: `method`

Reads or writes a `data-` attribute.

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

Returns: `String|Macro` The value, or the macro when `value` is passed.

Example: Using attrd()
```js
macro.attrd('id', '5');
console.log(macro.attr('data-id'));  // 5
```

### self.attrd2(name)

Type: `method`

Reads a `data-` attribute from the element or its closest parent that has it.

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

Returns: `String` The value.

Example: Using attrd2()
```js
console.log(macro.attrd2('area'));  // main
```

### self.css(name, [value])

Type: `method`

Reads or writes a CSS property.

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

Returns: `String|Macro` The value, or the macro when `value` is passed.

Example: Using css()
```js
macro.css('display', 'block');
console.log(macro.css('display'));  // block
```

### self.visible([callback])

Type: `method`

Tells whether the element is visible.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Runs when the element is visible. |

Returns: `Boolean` The current visibility.

With a callback, the function runs as soon as the element is visible.

Example: Using visible()
```js
console.log(macro.visible());  // true
```

### self.width([callback])

Type: `method`

Returns the width of the element in pixels.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Receives the width. |

Returns: `Number` The width.

With a callback, the function runs as soon as the width is greater than zero.

Example: Using width()
```js
macro.css('display', 'block');
macro.css('width', '200px');
console.log(macro.width());  // 200
```

### self.height([callback])

Type: `method`

Returns the height of the element in pixels.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Receives the height. |

Returns: `Number` The height.

With a callback, the function runs as soon as the height is greater than zero.

Example: Using height()
```js
macro.css('display', 'block');
macro.css('height', '50px');
console.log(macro.height());  // 50
```

### self.autofocus([selector])

Type: `method`

Moves the focus to the first visible field.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | no | `input,select,textarea` | The fields. |

Returns: `Macro` The macro.

The method does nothing on mobile devices.

Example: Using autofocus()
```js
macro.autofocus();
console.log(document.activeElement.tagName);  // INPUT
```

### self.faicon(value)

Type: `method`

Completes the name of an icon.

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

Returns: `String` The CSS classes.

A name without a space receives `DEF.iconprefix`, which is `ti ti-`.

Example: Using faicon()
```js
console.log(macro.faicon('home'));  // ti ti-home
```
