JQuery: save extra / additional data / information inside jQuery object?

Is it possible and smarter to store additional data inside a jQuery object?

Now I have objects containing some data, but these objects also have a visual representation of this data. This works, but I have a lot of code to sync them.

For example, if you delete an object from dom, I must also remove the associated object from the array of objects. Removing is pretty simple, but it gets a little more complicated if I start sorting / moving objects around.

+3
source share
1 answer

You can use $.data()for this :)

For instance:

$.data(element, 'varName', value);      //store
var value = $.data(element, 'varName'); //get

.data():

$("#ElementID").data('varName', value);      //store
var value = $("#ElementID").data('varName'); //get

, $.cache ( ), this[$.expando].

, .empty(), , .remove(), . .removeData() $.removeData(), .

+11

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


All Articles