Pavel Voronyuk2016-03-18 15:36:31
Pavel Voronyuk, 2016-03-18 15:36:31

Any good examples of using data-*?

Prompt good tutorials or examples of using data-* attributes.
What is generally good to use and where to apply?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
yuras666, 2016-03-18

The most convenient way to work with them is through jquery data - this will solve the problem of compatibility with all browsers.
For example, we have buttons on click on which we should open popups:

<button type="button" data-open-dialog="#bs-example-1-dialog">Окно 1</button>
<button type="button" data-open-dialog="#bs-example-1-dialog">Окно 1</button>
<button type="button" data-open-dialog="#bs-example-1-dialog">Окно 1</button>

Further, we can describe only one handler in js:
$(document).on('click', '[data-open-dialog]', function(){

In addition, you can store any json in data, giving an example with the same popup, you can store object data and build a popup from it after clicking on the button.
For another example, jquery plugins often flag that they are already set on an element to avoid reinitialization. Let's say $('#search-input').autocomplete() on the first initialization will write to the element $('#search-input').data('isAutocompleteInstall', true) and initialize the element, and on the second call it will no longer be initialized by new. Something like this, if I understood your question correctly.

Peter, 2016-03-18

In input form elements, for further validation.
there is a data-required attribute, which means the field is required, for information we can display the contents of the attribute.

Pavel Voronyuk, 2016-03-18

Peter , what is the advantage of using your example from this example?
at what data-required skips further, but required does not.

Sergey, 2016-03-18

I use it when I need to transfer data with Ajax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question