Data Attributes API

October CMS Documentation Docs

Data Attributes API

The data attributes API lets you issue AJAX requests without any JavaScript. In many cases the data attributes API is less verbose than the JavaScript API - you write less code to get the same result. The supported AJAX data attributes are:

AttributeDescription
data-requestspecifies the AJAX handler name.
data-request-confirmspecifies a confirmation message. The confirmation is displayed before the request is sent. If the user clicks the Cancel button the request isn't sent.
data-request-redirectspecifies a URL to redirect the browser after the successful AJAX request.
data-request-urlspecifies a URL to which the request is sent. default: window.location.href
data-request-updatespecifies a list of partials and page elements (CSS selectors) to update. The format is as follows: partial: selector, partial: selector. Usage of quotes is required in some cases, for example: 'my-partial': '#myelement'. If the selector string is prepended with the @ symbol, the content received from the server will be appended to the element, instead of replacing the existing content. If the selector string is prepended with the ^ symbol, the content will be prepended instead.
data-request-ajax-globalfalse by default. Set true to enable jQuery ajax events (opens new window) globally : ajaxStart, ajaxStop, ajaxComplete, ajaxError, ajaxSuccess and ajaxSend.
data-request-dataspecifies additional POST parameters to be sent to the server. The format is following: var: value, var: value. Use quotes if needed: var: 'some string'. The attribute can be used on the triggering element, for example on the button that also has the data-request attribute, on the closest element of the triggering element and on the parent form element. The framework merges values of the data-request-data attributes. If the attribute on different elements defines parameters with the same name, the framework uses the following priority: the triggering element data-request-data, the closer parent elements data-request-data, the form input data.
data-request-before-updatespecifies JavaScript code to execute directly before the page contents are updated.
data-request-successspecifies JavaScript code to execute after the request is successfully completed.
data-request-errorspecifies JavaScript code to execute if the request encounters an error.
data-request-completespecifies JavaScript code to execute if the request is successfully completed or encounters an error.
data-request-loadingspecifies a CSS selector for an element to be displayed while the request runs. You can use this option to show the AJAX loading indicator. The feature uses jQuery's show() and hide() functions to manage the element visibility.
data-request-formexplicitly specify a form element to use for sourcing the form data. If this is unspecified, the closest form to the triggering element is used, including if the element itself is a form.
data-request-flashwhen specified this option instructs the server to clear and send any flash messages with the response. This option is also used by the extra features.
data-request-fileswhen specified the request will accept file uploads, this requires FormData interface support by the browser.
data-browser-validatewhen specified browser-based client side validation will run on the request before it submits.
data-track-inputcan be applied to a text, number, or password input field that also has the data-request attribute. When defined, the input field automatically sends an AJAX request when a user types something in the field. The optional attribute value can define the interval, in milliseconds, the framework waits before it sends the requests.

When the data-request attribute is specified for an element, the element triggers an AJAX request when a user interacts with it. Depending on the type of element, the request is triggered on the following events:

ElementEvent
Formswhen the form is submitted.
Links, buttonswhen the element is clicked.
Text, number, and password fieldswhen the text is changed and only if the data-track-input attribute is presented.
Dropdowns, checkboxes, radioswhen the element is selected.

# Usage Examples

Trigger the onCalculate handler when the form is submitted. Update the element with the identifier "result"` with the calcresult partial:

<form data-request="onCalculate" data-request-update="calcresult: '#result'">

Request a confirmation when the Delete button is clicked before the request is sent:

<form ... >
    ...
    <button data-request="onDelete" data-request-confirm="Are you sure?">Delete</button>

Redirect to another page after the successful request:

<form data-request="onLogin" data-request-redirect="/admin">

Show a popup window after the successful request:

<form data-request="onLogin" data-request-success="alert('Yay!')">

Send a POST parameter mode with a value update:

<form data-request="onUpdate" data-request-data="mode: 'update'">

Send a POST parameter id with value 7 across multiple elements:

<div data-request-data="id: 7">
    <button data-request="onDelete">Delete</button>
    <button data-request="onSave">Update</button>
</div>

Including file uploads with a request:

<form data-request="onSubmit" data-request-files>
    <input type="file" name="photo" accept="image/*" />
    <button type="submit">Submit</button>
</form>
On This Page