Using the library
It is possible to use PowerButtons in a declarative way (i.e. including parameters in the html5 tags), or programmatically in a script either using vanilla javascript or jQuery.
Using the library in a declarative way
In most of cases the prefered method is to use the library in a declarative way, i.e. adding the parameters in the html5 tags. This is the way I use the library in my own projects, so that I can add the functionality to the buttons without writing any javascript code.
The following example shows how to show a message when the user clicks on a button, before submitting a form.
<button type="submit" data-showmessage="show a message">Send the form</button>
Note
Using the library in this way has the limitation that the order of application of the plugins is fixed, and the fact that a plugin can only be applied to a button once (see details). In case of complex requirements, it is better to use the library programmatically.
Vanilla javascript API
The library can also be used programmatically using vanilla javascript. Using this method, the library can be used in a more flexible way, but it requires a bit of code to add the functionality to the buttons.
The library exports a global function called window.powerButtons:
- window.powerButtons(pluginName, els = [], options = {})
This function is used to add actions to the buttons programmatically. In particular, it applies the plugin
pluginNameto the elementselswith the optionsoptions.- Arguments:
pluginName (
string()) – the name of the plugin to apply (e.g.confirm,showmessage, etc.)els (
string|HTMLElement|array[HTMLElement]|array[string]()) – the elements to which the plugin will be applied. If it is a string (or an array of strings), each one is interpreted as a selector. If it is an HTMLElement (or an array of HTMLElements), each element is interpreted as a single element.options (
object()) – the options to pass to the plugin (the options depend on the plugin)
- Returns:
the elements to which the plugin has been applied
For example:
<button id="mybutton">Send the form</button>
<script>
powerButtons('confirm', '#mybutton', {confirm: "Are you sure?"});
</script>
- window.powerButtons.version
This attribute contanins the version of the library.
- window.powerButtons.plugins()
This function returns the list of the available plugins.
- Returns:
the list of the available plugins (in the order that they are applied)
jQuery API
The library can also be used programmatically using jQuery. Using this method is similar to using the library with vanilla javascript, but it integrates with jQuery.
The library exports a jQuery function called jQuery.fn.powerButtons:
- $.fn.powerButtons(pluginName, options = {})
This function applies the plugin
pluginNameto the jQuery elements, with the optionsoptions.- Arguments:
pluginName (
string()) – the name of the plugin to apply (e.g.confirm,showmessage, etc.)options (
object()) – the options to pass to the plugin (the options depend on the plugin)
- Returns:
the elements to which the plugin has been applied
For example:
<button id="mybutton">Send the form</button>
<script>
$('#mybutton').powerButtons('confirm', {confirm: "Are you sure?"});
</script>
- $.fn.powerButtons.version
This attribute contanins the version of the library.
- $.fn.powerButtons.plugins()
This function returns the list of the available plugins.
- Returns:
the list of the available plugins (in the order that they are applied)