Click actions
How to call functions, plugins and components from the markup on a click, a double click and a right click.
On this page
A button does not need an event handler of its own. Give it the class exec and write what to call in the attribute data-exec. It works in the whole page, also in markup that is rendered or imported later. The attributes are listed in the reference.
Calling a function
<button class="exec" data-id="15" data-exec="FUNC.remove">Remove</button>
<script>
FUNC.remove = function(element, e) {
console.log('remove', element.attrd('id'));
};
</script>A click prints remove 15. The function receives the element as a jQuery object and the event. Use <button> or <a> elements when you can, because they also work with the keyboard.
Reading data from a parent
In a list, the identifier is often on the row and the buttons are inside it. ATTRD(element) reads data-id from the element or from its closest parent that has it:
<div data-id="15">
<b>Lamp</b>
<button class="exec" data-exec="FUNC.edit">Edit</button>
</div>
<script>
FUNC.edit = function(element) {
console.log('edit', ATTRD(element)); // edit 15
};
</script>Calling a plugin or a component
Inside a plugin, ?/method calls a method of that plugin. Inside a component, @method calls a method of that component, which receives the component as its first argument.
<ui-plugin path="cart">
<button id="clear" class="exec" data-exec="?/clear">Clear</button>
</ui-plugin>
<ui-component name="panel">
<button id="close" class="exec" data-exec="@close">Close</button>
</ui-component>
<script>
PLUGIN('cart', function(exports) {
exports.clear = function(element, e) {
console.log('clear', exports.name);
};
});
COMPONENT('panel', function(self) {
self.close = function(component, element, e) {
console.log('close', component.name);
};
});
</script>A click on each button prints:
clear cart
close panelChanging the URL
data-href navigates with REDIRECT(), without reloading the page. It can be used alone or together with data-exec. Read more in Routing.
<button class="exec" data-href="/orders/">Orders</button>A link keeps its default action, so the browser follows its href. Add data-prevent="true" to stop it:
<a class="exec" data-exec="FUNC.help" data-prevent="true" href="/help/">Help</a>Double click and right click
The class exec2 handles a double click with data-exec2, and the class exec3 handles a click with the right button with data-exec3:
<div id="file" class="exec exec2 exec3" data-exec="FUNC.select" data-exec2="FUNC.open" data-exec3="FUNC.menu">report.pdf</div>
<script>
FUNC.select = element => console.log('select');
FUNC.open = element => console.log('open');
FUNC.menu = element => console.log('menu');
</script>A click prints select after 300 milliseconds, the time the library waits for a second click. A double click prints only open. A right click prints menu, and the menu of the browser is not displayed.