Spanish Total.js
English
Documentation For AI agents
Contents

jQuery extensions

Methods that jComponent adds to jQuery for working with classes, attributes, SVG, components and scopes.

.md GitHub
On this page

jComponent includes jQuery and adds these methods to $.fn, so they work on every jQuery object. The Component page describes the objects returned by the component methods, and Using components explains how components are declared.

API

method

el.aclass(name, [delay])

Adds one or more classes, immediately or after a delay.
ParameterTypeDescription
name String The class names, separated by spaces.
delay? Number The delay in milliseconds.

Returns jQuery The same element.

Delayed calls share one timer per element, so a new delayed aclass(), rclass() or rclass2() call cancels the one still waiting.

Add a class
<div id="box"></div>

<script>
var el = $('#box');
el.aclass('active');
el.hclass('active'); // true

el.aclass('later', 50);
el.hclass('later'); // false

setTimeout(function() {
	console.log(el.hclass('later'));
}, 100);
</script>
method

el.asvg(tag)

Appends an SVG element to the element.
ParameterTypeDescription
tag String A tag name, or SVG markup.

Returns jQuery The new element, when tag is a tag name.

A tag name creates one element in the SVG namespace and returns it. A text with markup appends all of its elements; use a tag name when you need the new element afterwards.

Append SVG elements
<svg id="chart"></svg>

<script>
var svg = $('#chart');

svg.asvg('circle').attr('r', 5);
svg.asvg('<rect width="3"/><line/>');

svg.children().length; // 3
svg.children()[0].namespaceURI; // "http://www.w3.org/2000/svg"
</script>
method

el.attrd(name, [value])

Reads or writes a data- attribute.
ParameterTypeDescription
name String The attribute name without the data- prefix.
value? Object The new value.

Returns String or jQuery The value when reading, the same element when writing.

Data attributes
<div id="box" data-id="7"></div>

<script>
var el = $('#box');

el.attrd('id'); // "7"
el.attrd('name', 'Ann');
el.attr('data-name'); // "Ann"
</script>
method

el.attrd2(name)

Reads a data- attribute from the element or its nearest parent that has it.
ParameterTypeDescription
name String The attribute name without the data- prefix.

Returns String The value, or undefined.

Inherit a value
<div data-id="7"><span id="child"></span></div>

<script>
$('#child').attrd2('id'); // "7"
$('#child').attrd2('missing'); // undefined
</script>
method

el.autofocus([selector])

Moves the focus to the first visible field inside the element.
ParameterTypeDescription
selector? String The fields to look for. Default: 'input,select,textarea'

Returns jQuery The same element.

When no field can take the focus, the method retries every 200 milliseconds, up to 15 times. It does nothing on mobile devices.

Focus a form
<div id="form">
	<input id="hidden" style="display:none">
	<input id="name">
	<textarea id="note"></textarea>
</div>

<script>
$('#form').autofocus();
document.activeElement.id; // "name"

$('#form').autofocus('textarea');
document.activeElement.id; // "note"
</script>
method

el.binder()

Returns the binder of the element or its nearest parent.

Returns Object The binder, or null.

See ui-bind.

Read a binder
<ui-bind path="user.name" config="show"><span id="label"></span></ui-bind>

<script>
setTimeout(function() {
	console.log($('#label').binder().path);
}, 500);
</script>
method

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

Runs a command in all components inside the element.
ParameterTypeDescription
name String The command name.
a? Object The first of up to five arguments for the command.

Components register commands with self.command(). See Component.

Run a command
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', function(self) {
	self.make = function() {
		self.command('reset', function(value) {
			console.log(self.path, value);
		});
	};
});

ON('ready', function() {
	$('#panel').CMD('reset', 0);
});
</script>
method

el.component()

Returns the component of the element or its nearest parent.

Returns Component The component, or null.

Find the parent component
<ui-component name="counter" path="data.a"><b id="inner"></b></ui-component>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	console.log($('#inner').component().path);
});
</script>
method

el.components([fn])

Lists or walks the ready components inside the element.
ParameterTypeDescription
fn? Function Called for each component.

Returns Array or jQuery Without fn, an array of components, or null when there are none. With fn, the component elements.

With fn, the function is called with the component as this and the position of its element as the argument.

Walk components
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	console.log($('#panel').components().length);
	$('#panel').components(function(index) {
		console.log(index, this.path);
	});
});
</script>
method

el.EXEC(name, [args...])

Calls a method on the components inside the element.
ParameterTypeDescription
name String The method name, optionally with the prefix ^.
args? Object The arguments of the method.

Returns jQuery The same element.

With the prefix ^, the method is called on the components in the parent elements. Components without the method are skipped.

Call a method
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	$('#panel').EXEC('hello', 'Ann');
});
</script>
Call a method on the parents
<ui-component name="counter" path="data.a"><b id="inner"></b></ui-component>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	$('#inner').EXEC('^hello', 'Bob');
});
</script>
method

el.FIND(selector, [many], [callback], [timeout])

Finds components inside the element.
ParameterTypeDescription
selector String The component selector.
many? Boolean Use true to get all matching components.
callback? Function Receives the result when it is available.
timeout? Number The maximum waiting time in milliseconds.

Returns Component, Array or jQuery The first component, or an array with many. With callback, the same element.

The selector can be a component name, name[path], .path, #id or *.

With callback, the method waits until a component exists and then calls callback(component), with the component also as this. When timeout runs out first, the callback is not called. many can be left out when you pass a callback.

Find components
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	var panel = $('#panel');
	console.log(panel.FIND('counter').path);
	console.log(panel.FIND('counter', true).length);
	console.log(panel.FIND('.data.b').path);
	console.log(panel.FIND('counter[data.b]').path);
});
</script>
Wait for a component
<div id="panel"></div>

<script>
$('#panel').FIND('late', function(component) {
	console.log('found', component.path);
}, 3000);

COMPONENT('late', function(self) {});
$('#panel').append('<ui-component name="late" path="data.late"></ui-component>');
COMPILE();
</script>
method

el.hclass(name)

Checks whether the element has a class.
ParameterTypeDescription
name String The class name.

Returns Boolean true when the class is present.

Check a class
<div id="box" class="active"></div>

<script>
$('#box').hclass('active'); // true
$('#box').hclass('missing'); // false
</script>
method

el.multiple(selectors)

Runs several find() calls and returns the results under their names.
ParameterTypeDescription
selectors Object Names and selectors.

Returns Object jQuery objects under the same names.

Find several elements
<div id="form"><input id="name"><button>Save</button></div>

<script>
var ui = $('#form').multiple({ name: 'input', save: 'button' });

ui.name.attr('id'); // "name"
ui.save.text(); // "Save"
</script>
method

el.plugin()

Returns the plugin that contains the element.

Returns Plugin The plugin, or undefined.

See Plugin.

Reach a plugin
<ui-plugin path="orders"><div id="inner"></div></ui-plugin>

<script>
PLUGIN('orders', function(exports) {
	exports.total = function() {
		return 3;
	};
});

setTimeout(function() {
	console.log($('#inner').plugin().total());
}, 500);
</script>
method

el.psvg(tag)

Prepends an SVG element to the element.
ParameterTypeDescription
tag String A tag name, or SVG markup.

Returns jQuery The new element, when tag is a tag name.

It works like asvg(), and the new content goes first.

Prepend an SVG element
<svg id="chart"><circle r="5"></circle></svg>

<script>
var svg = $('#chart');

svg.psvg('text').attr('x', 1);
svg.children()[0].nodeName; // "text"
</script>
method

el.rattr(name)

Removes an attribute.
ParameterTypeDescription
name String The attribute name.

Returns jQuery The same element.

Remove an attribute
<div id="box" title="Hello"></div>

<script>
$('#box').rattr('title').attr('title'); // undefined
</script>
method

el.rattrd(name, [name2], ...)

Removes one or more data- attributes.
ParameterTypeDescription
name String The attribute names without the data- prefix.

Returns jQuery The same element.

Remove data attributes
<div id="box" data-a="1" data-b="2" data-c="3"></div>

<script>
var el = $('#box').rattrd('a', 'b');

el.attr('data-a'); // undefined
el.attr('data-b'); // undefined
el.attr('data-c'); // "3"
</script>
method

el.rclass([name], [delay])

Removes classes, immediately or after a delay.
ParameterTypeDescription
name? String The class names, separated by spaces.
delay? Number The delay in milliseconds.

Returns jQuery The same element.

Without a name, all classes are removed. Delayed calls share one timer per element, so a new delayed aclass(), rclass() or rclass2() call cancels the one still waiting.

Remove classes
<div id="box" class="a b c"></div>

<script>
var el = $('#box');

el.rclass('a').attr('class'); // "b c"
el.rclass().attr('class'); // ""
</script>
method

el.rclass2(search, [delay])

Removes the classes that contain a text or match a regular expression.
ParameterTypeDescription
search String or RegExp The text to look for, or a regular expression.
delay? Number The delay in milliseconds.

Returns jQuery The same element.

Delayed calls share one timer per element, so a new delayed aclass(), rclass() or rclass2() call cancels the one still waiting.

Remove by pattern
<div id="box" class="size-1 size-2 color-red ok"></div>

<script>
var el = $('#box');

el.rclass2('size-').attr('class'); // "color-red ok"
el.rclass2(/^color/).attr('class'); // "ok"
</script>
method

el.RECONFIGURE(selector, value)

Changes the configuration of components inside the element.
ParameterTypeDescription
selector String The component selector.
value String or Object The new configuration.

Returns jQuery The same element.

The selector has the same forms as in FIND().

Reconfigure components
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', 'step:1', function(self) {});

ON('ready', function() {
	var panel = $('#panel');
	panel.RECONFIGURE('counter', 'step:5');
	console.log(panel.FIND('counter').config.step);
	panel.RECONFIGURE('counter', { step: 9 });
	console.log(panel.FIND('counter').config.step);
});
</script>
method

el.scope()

Returns the scope that contains the element.

Returns Scope The scope. Outside a scope, the result is empty.

See Scope.

Read the scope path
<ui-plugin path="orders"><div id="inner"></div></ui-plugin>

<script>
PLUGIN('orders', function(exports) {});

setTimeout(function() {
	console.log($('#inner').scope().path);
}, 500);
</script>
method

el.SETTER([wait], selector, method, [args...])

Calls a method on the components inside the element that match a selector.
ParameterTypeDescription
wait? Boolean Use true to wait for the component.
selector String The component selector.
method String The method name.
args? Object The arguments of the method.

Returns jQuery The same element.

The selector has the same forms as in FIND(). The method can be a nested name such as nested.run, and it can also be part of the selector as selector/method. With true as the first argument, the call waits until a matching component exists.

Call matching components
<div id="panel">
	<ui-component name="counter" path="data.a"></ui-component>
	<ui-component name="counter" path="data.b"></ui-component>
</div>

<script>
COMPONENT('counter', function(self) {
	self.hello = function(name) {
		console.log(self.path, 'hello', name);
	};
});

ON('ready', function() {
	$('#panel').SETTER('counter', 'hello', 'Ann');
	$('#panel').SETTER('.data.b/hello', 'Bob');
});
</script>
Wait for a component
<div id="panel"></div>

<script>
$('#panel').SETTER(true, 'late', 'hello', 'Ann');

COMPONENT('late', function(self) {
	self.hello = function(name) {
		console.log('hello', name);
	};
});

$('#panel').append('<ui-component name="late" path="data.late"></ui-component>');
COMPILE();
</script>
method

el.tclass(name, [state])

Toggles a class.
ParameterTypeDescription
name String The class name.
state? Boolean true adds the class and false removes it.

Returns jQuery The same element.

Toggle a class
<div id="box"></div>

<script>
var el = $('#box');

el.tclass('open').hclass('open'); // true
el.tclass('open').hclass('open'); // false
el.tclass('open', true).hclass('open'); // true
</script>
method

el.vbind()

Returns the virtual binder of an item created by VBINDARRAY().

Returns Object The binder of the item, or null.

The element can be the item or an element inside it. See Virtual binder.

Read the item binder
<div id="list"></div>

<script>
var list = VBINDARRAY('<div><b data-bind="name__text"></b></div>', $('#list'));
list.set([{ name: 'Ann' }, { name: 'Bob' }]);

$('#list b').last().vbind().index; // 1
</script>
method

el.vbindarray()

Returns the object created by VBINDARRAY() for the element or its nearest parent.

Returns Object The virtual binder array, or null.

Read the array binder
<div id="list"></div>

<script>
var list = VBINDARRAY('<div><b data-bind="name__text"></b></div>', $('#list'));
list.set([{ name: 'Ann' }]);

$('#list b').vbindarray() === list; // true
</script>
property

$.components

A reference to the global object M.

See M.

The same object
$.components === M; // true
Made in Europe