Spanish Total.js
English
Documentation For AI agents
Contents

jQuery extensions

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

.md GitHub
On this page

jComponent includes jQuery and adds these methods to every jQuery selection. They are mostly used inside components and plugins: see Creating components and Plugins.

API

method

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

Adds a CSS class.
ParameterTypeDescription
name String The class name.
delay? Number 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.

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

$(selector).asvg(tag)

Appends an SVG element.
ParameterTypeDescription
tag String 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.

Drawing
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>
method

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

Reads or writes a data- attribute.
ParameterTypeDescription
name String The attribute name without data-.
value? Object The value to write.

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

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

$(selector).attrd2(name)

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

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

The search ends at <body>.

Searching the parents
<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>
method

$(selector).autofocus([selector])

Moves the focus to the first visible field inside the selection.
ParameterTypeDescription
selector? String The fields to search. Default: input,select,textarea

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.

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

$(selector).binder()

Returns the closest 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.

Finding the binder
<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>
method

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

Runs a command on the components inside the selection.
ParameterTypeDescription
name String The command, optionally followed by flags.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

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

A command in one panel
<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>
method

$(selector).component()

Returns the closest 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.

Finding the component
<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>
method

$(selector).COMPONENTS()

Returns the ready components inside the selection.

Returns Array The Component instances.

Listing components
<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>
method

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

Runs a function, a plugin method, an event or a component setter.
ParameterTypeDescription
name String What to run.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

The form of name selects what runs:

NameWhat runs
myfunctionA global function
FUNC.pathThe function at a path
plugin/methodA method of a plugin
?/method, |methodA method of the closest plugin
#nameThe event name
-component/methodA component setter

The method returns nothing.

Running a function
<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>
method

$(selector).hclass(name)

Checks if the element has a CSS class.
ParameterTypeDescription
name String The class name.

Returns Boolean true when the class is present.

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

$(selector).multiple(selectors)

Runs several searches inside the selection at once.
ParameterTypeDescription
selectors Object The result keys and their selectors.

Returns Object A jQuery selection for each key.

Finding elements
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
method

$(selector).plugin()

Returns the closest 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.

Finding the plugin
<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>
method

$(selector).psvg(tag)

Inserts an SVG element at the start of the selection.
ParameterTypeDescription
tag String 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.

Inserting first
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>
method

$(selector).rattr(name)

Removes an attribute.
ParameterTypeDescription
name String The attribute name.

Returns jQuery The same selection.

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

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

Removes data- attributes.
ParameterTypeDescription
name String The attribute name without data-. Each further argument removes one more attribute.

Returns jQuery The same selection.

Removing attributes
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>
method

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

Removes a CSS class, or all of them.
ParameterTypeDescription
name? String The class name.
delay? Number 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().

Removing classes
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
method

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

Removes all the CSS classes that contain a text or match a pattern.
ParameterTypeDescription
search String or RegExp The text to find or the pattern to test.
delay? Number The delay in milliseconds.

Returns jQuery The same selection.

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

Removing by text or pattern
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
method

$(selector).scrollbar()

Returns the closest custom scrollbar created with SCROLLBAR().

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.

Finding the scrollbar
<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>
method

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

Runs a function when it exists, otherwise writes a value to a path.
ParameterTypeDescription
name String The function or the path.
a? Object The first argument, or the value for the path.
b? Object The second argument.
c? Object The third argument.
d? Object 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(). The method returns nothing.

Function or path
<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>
method

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

Calls a method of the components inside the selection.
ParameterTypeDescription
name String The components and the method.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object 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.

Calling components
<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>
method

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

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

Returns jQuery The same selection.

Toggling
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
Made in Europe