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

# jQuery extensions

> Methods that jComponent adds to jQuery for classes, attributes, SVG, finding instances and calling the library.

jComponent includes jQuery and adds these methods to every jQuery selection. They are mostly used inside components and plugins: see [Creating components](../../guides/creating-components/) and [Plugins](../../guides/plugins/).

## API

### $(selector).aclass(name, [delay])

Type: `method`

Adds a CSS class.

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

Returns: `jQuery` The same selection.

With `delay`, the class is added later. Each element holds one delayed class change: a new delayed call from `aclass()`, `rclass()` or `rclass2()` replaces the one that is waiting.

Example: Adding a class
```js
var el = $('<div>');
el.aclass('active');
console.log(el.attr('class')); // active
```

Example: Adding a class later
```js
var el = $('<div>');
el.aclass('active', 50);
console.log(el.hclass('active')); // false
setTimeout(function() {
	console.log(el.hclass('active')); // true
}, 100);
```

### $(selector).asvg(tag)

Type: `method`

Appends an SVG element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `tag` | `String` | yes |  | A tag name or SVG markup. |

Returns: `jQuery` The new element when `tag` is a tag name.

With a tag name, the method creates the element in the SVG namespace and returns it. `tag` can also be SVG markup, which is appended to the selection.

Example: Drawing
```js
var svg = $('<svg></svg>');
var circle = svg.asvg('circle');
circle.attr('r', 5);
svg.asvg('<rect width="5"></rect>');
console.log(circle[0].namespaceURI); // http://www.w3.org/2000/svg
console.log(svg.html()); // <circle r="5"></circle><rect width="5"></rect>
```

### $(selector).attrd(name, [value])

Type: `method`

Reads or writes a `data-` attribute.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The attribute name without `data-`. |
| `value` | `Object` | no |  | The value to write. |

Returns: `String or jQuery` The value when reading, or the same selection when writing.

Example: Reading and writing
```js
var el = $('<div data-id="7">');
console.log(el.attrd('id')); // 7
el.attrd('name', 'box');
console.log(el.attr('data-name')); // box
```

### $(selector).attrd2(name)

Type: `method`

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

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

Returns: `String` The value, or `undefined` when no element has the attribute.

The search ends at `<body>`.

Example: Searching the parents
```html
<div data-id="7"><span><b id="item"></b></span></div>
<script>
console.log($('#item').attrd2('id')); // 7
console.log($('#item').attrd2('missing') === undefined); // true
</script>
```

### $(selector).autofocus([selector])

Type: `method`

Moves the focus to the first visible field inside the selection.

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

Returns: `jQuery` The same selection.

The method tries again for a short time when no field can take the focus yet. It does nothing on mobile devices.

Example: Focusing a form
```html
<div id="form"><input id="code" type="hidden"><input id="name"></div>
<script>
$('#form').autofocus();
console.log(document.activeElement.id); // name
</script>
```

### $(selector).binder()

Type: `method`

Returns the closest [ui-bind](../ui-bind/) instance.

Returns: `Object` The instance, or `undefined` when there is none.

The search starts at the first element of the selection and goes through its parents.

Example: Finding the binder
```html
<ui-bind id="title" path="common.title" config="text"></ui-bind>
<script>
setTimeout(function() {
	console.log($('#title').binder().element[0].tagName); // UI-BIND
	console.log($('body').binder() === undefined); // true
}, 300);
</script>
```

### $(selector).CMD(name, [a], [b], [c], [d])

Type: `method`

Runs a command on the components inside the selection.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The command, optionally followed by flags. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

Only the components nested in the selected elements are reached. Components register commands with [command()](../component/), and the global [CMD()](../globals/) reaches all of them. The method returns nothing.

Example: A command in one panel
```html
<div id="left"><ui-component name="list" id="a"></ui-component></div>
<div id="right"><ui-component name="list" id="b"></ui-component></div>
<script>
COMPONENT('list', function(self) {
	self.command('clear', () => console.log('clear', self.element.attr('id')));
});
ON('ready', function() {
	$('#left').CMD('clear'); // clear a
});
</script>
```

### $(selector).component()

Type: `method`

Returns the closest [Component](../component/) instance.

Returns: `Component` The instance, or `undefined` when there is none.

The search starts at the first element of the selection and goes through its parents.

Example: Finding the component
```html
<ui-component name="note" path="common.note"><b id="label"></b></ui-component>
<script>
COMPONENT('note', function(self) {});
setTimeout(function() {
	console.log($('#label').component().name); // note
	console.log($('body').component() === undefined); // true
}, 300);
</script>
```

### $(selector).COMPONENTS()

Type: `method`

Returns the ready components inside the selection.

Returns: `Array` The [Component](../component/) instances.

Example: Listing components
```html
<div id="left"><ui-component name="note" path="common.a"></ui-component></div>
<div id="right"><ui-component name="note" path="common.b"></ui-component></div>
<script>
COMPONENT('note', function(self) {});
setTimeout(function() {
	console.log($('body').COMPONENTS().length); // 2
	console.log($('#left').COMPONENTS()[0].path.toString()); // common.a
}, 300);
</script>
```

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

Type: `method`

Runs a function, a plugin method, an event or a component setter.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | What to run. |
| `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 of `name` selects what runs:

| Name | What runs |
| --- | --- |
| `myfunction` | A global function |
| `FUNC.path` | The function at a path |
| `plugin/method` | A method of a plugin |
| `?/method`, `\|method` | A method of the closest plugin |
| `#name` | The event `name` |
| `-component/method` | A component setter |

The method returns nothing.

Example: Running a function
```html
<ui-plugin path="orders"><button id="save"></button></ui-plugin>
<script>
var out = [];
PLUGIN('orders', function(exports) {
	exports.save = id => out.push('save ' + id);
});
FUNC.hello = name => out.push('hello ' + name);
ON('saved', id => out.push('event ' + id));
setTimeout(function() {
	var button = $('#save');
	button.EXEC('FUNC.hello', 'Ana');
	button.EXEC('orders/save', 1);
	button.EXEC('?/save', 2);
	button.EXEC('|save', 3);
	button.EXEC('#saved', 4);
	console.log(out); // ["hello Ana","save 1","save 2","save 3","event 4"]
}, 300);
</script>
```

### $(selector).hclass(name)

Type: `method`

Checks if the element has a CSS class.

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

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

Example: Checking a class
```js
var el = $('<div class="active">');
console.log(el.hclass('active')); // true
console.log(el.hclass('hidden')); // false
```

### $(selector).multiple(selectors)

Type: `method`

Runs several searches inside the selection at once.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selectors` | `Object` | yes |  | The result keys and their selectors. |

Returns: `Object` A jQuery selection for each key.

Example: Finding elements
```js
var el = $('<div><input><input><button></button></div>');
var found = el.multiple({ fields: 'input', submit: 'button' });
console.log(found.fields.length); // 2
console.log(found.submit.length); // 1
```

### $(selector).plugin()

Type: `method`

Returns the closest [Plugin](../plugin/) instance.

Returns: `Plugin` The instance, or `undefined` when there is none.

The search starts at the first element of the selection and goes through its parents.

Example: Finding the plugin
```html
<ui-plugin path="orders"><b id="label"></b></ui-plugin>
<script>
PLUGIN('orders', function(exports) {});
setTimeout(function() {
	console.log($('#label').plugin().name); // orders
	console.log($('body').plugin() === undefined); // true
}, 300);
</script>
```

### $(selector).psvg(tag)

Type: `method`

Inserts an SVG element at the start of the selection.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `tag` | `String` | yes |  | A tag name or SVG markup. |

Returns: `jQuery` The new element when `tag` is a tag name.

With a tag name, the method creates the element in the SVG namespace and returns it. `tag` can also be SVG markup, which is inserted at the start of the selection.

Example: Inserting first
```js
var svg = $('<svg><line></line></svg>');
var group = svg.psvg('g');
svg.psvg('<path></path>');
console.log(group[0].tagName); // g
console.log(svg.html()); // <path></path><g></g><line></line>
```

### $(selector).rattr(name)

Type: `method`

Removes an attribute.

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

Returns: `jQuery` The same selection.

Example: Removing an attribute
```js
var el = $('<div title="x" id="a">');
el.rattr('title');
console.log(el[0].outerHTML); // <div id="a"></div>
```

### $(selector).rattrd(name, [name2], [...])

Type: `method`

Removes `data-` attributes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The attribute name without `data-`. Each further argument removes one more attribute. |

Returns: `jQuery` The same selection.

Example: Removing attributes
```js
var el = $('<div data-a="1" data-b="2" data-c="3">');
el.rattrd('a', 'b');
console.log(el[0].outerHTML); // <div data-c="3"></div>
```

### $(selector).rclass([name], [delay])

Type: `method`

Removes a CSS class, or all of them.

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

Returns: `jQuery` The same selection.

Without `name`, every class is removed. With `delay`, the class is removed later, and the delayed change works as in `aclass()`.

Example: Removing classes
```js
var el = $('<div class="a b c">');
el.rclass('a');
console.log(el.attr('class')); // b c
el.rclass();
console.log(el.attr('class') === ''); // true
```

### $(selector).rclass2(search, [delay])

Type: `method`

Removes all the CSS classes that contain a text or match a pattern.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `search` | `String or RegExp` | yes |  | The text to find or the pattern to test. |
| `delay` | `Number` | no |  | The delay in milliseconds. |

Returns: `jQuery` The same selection.

With `delay`, the classes are removed later, and the delayed change works as in `aclass()`.

Example: Removing by text or pattern
```js
var el = $('<div class="tab-1 tab-2 box">');
el.rclass2('tab-');
console.log(el.attr('class')); // box
var other = $('<div class="tab-1 tab-2 box">');
other.rclass2(/1$/);
console.log(other.attr('class')); // tab-2 box
```

### $(selector).scrollbar()

Type: `method`

Returns the closest custom scrollbar created with [SCROLLBAR()](../globals/).

Returns: `Object` The scrollbar instance, or `undefined` when there is none.

The search starts at the first element of the selection and goes through its parents.

Example: Finding the scrollbar
```html
<div id="area" style="height:50px"><p id="text"></p></div>
<script>
var scrollbar = SCROLLBAR($('#area'));
console.log($('#text').scrollbar() === scrollbar); // true
console.log($('body').scrollbar() === undefined); // true
</script>
```

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

Type: `method`

Runs a function when it exists, otherwise writes a value to a path.

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

A global function, `plugin/method` and `?/method` are called with the arguments. Any other name is a path, and `a` is written to it with [SET()](../globals/). The method returns nothing.

Example: Function or path
```html
<ui-plugin path="orders"><button id="save"></button></ui-plugin>
<script>
var out = [];
PLUGIN('orders', function(exports) {
	exports.save = (a, b) => out.push(a + b);
});
window.hello = name => out.push('hello ' + name);
setTimeout(function() {
	var button = $('#save');
	button.SEEX('hello', 'Ana');
	button.SEEX('?/save', 1, 2);
	button.SEEX('common.count', 5);
	console.log(out); // ["hello Ana",3]
	console.log(GET('common.count')); // 5
}, 300);
</script>
```

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

Type: `method`

Calls a method of the components inside the selection.

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

`name` is `component/method`. In place of the component name, `*` selects all the components and `.path` selects the components bound to that path. The method returns nothing.

Example: Calling components
```html
<div id="left"><ui-component name="note" path="common.a" config="label:A"></ui-component></div>
<div id="right"><ui-component name="note" path="common.b" config="label:B"></ui-component></div>
<script>
var out = [];
COMPONENT('note', function(self, config) {
	self.show = text => out.push(config.label + ' ' + text);
});
setTimeout(function() {
	$('#left').SETTER('note/show', 'one');
	$('body').SETTER('*/show', 'two');
	$('body').SETTER('.common.b/show', 'three');
	console.log(out); // ["A one","A two","B two","B three"]
}, 300);
</script>
```

### $(selector).tclass(name, [enable])

Type: `method`

Toggles a CSS class.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The class name. |
| `enable` | `Boolean` | no |  | `true` adds the class and `false` removes it. |

Returns: `jQuery` The same selection.

Example: Toggling
```js
var el = $('<div>');
el.tclass('open');
console.log(el.hclass('open')); // true
el.tclass('open');
console.log(el.hclass('open')); // false
el.tclass('open', true);
console.log(el.hclass('open')); // true
```
