What is the reason for storing data with .data () instead of using simple variables?

Let's say I have to remember the initial height of some element. The general practice that I see is to save this value using $.datafor this element.

I do not understand the benefits of this. Why not just save a simple variable with this value or an array with values ​​if there are multiple elements? Saves code that is easy to understand.

+4
source share
3 answers

It allows you to reuse a function to apply the same effect to other elements (without having to deal with closure).

It allows you to initialize a value using HTML.

.

+4

data() , , ,

$('.elements').on('click', function() {
     $(this).data('value', this.value);
});

$('.elements').on('keyup', function() {
    $(this).val( $(this).data('value') );
});

, , data() , , .

+6

, INSIDE A NODE, . NODE, node

0

Source: https://habr.com/ru/post/1546714/


All Articles