K
K
Kirill Tekord2018-02-18 16:26:52
JavaScript
Kirill Tekord, 2018-02-18 16:26:52

How to bind your object to HTMLElement'u (without jQuery)?

Question from a dinosaur in the JS world. There are several HTMLElements selected by the selector. To these elements, you need to bind an object that stores some additional parameters specific to these elements, for example, the old color and the new color, so that when you click on the button, these colors switch.
How to do it WITHOUT jQuery? The property of the dataset element can store only string values, and I did not find any more suitable fields for storing custom data. As an option, use WeakMap, where the key is a reference to an HTMLElement, and the value is a reference to an object.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Medin K, 2018-02-18
@tekord

it should work just fine.
html_element['my_custom_param'] = { my_val:1 };

P
Pavel Shvedov, 2018-02-18
@mmmaaak

If you decide on the forehead, then serialize the object into a json string, and store it in the dataset

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question