Mr. Noob Mr. Noob - 6 months ago 23
HTML Question

Storing data into elements

I wonder how can I store data into some html elements like div. I can store it in class name, id name, but how does it work with "data"? If I need to store someode id, name, years etc. And to get all that info by jquery.. Like for id

$(this).attr('id');
. Thanks

Answer

You can do this similarly like ID by adding data attribute in your DIV tag

<div id="elm" data-id="1" data-name="name" data-years="2016">

</div>

And then use jQuery like:

$(this).attr('data-id');
$(this).attr('data-name');
$(this).attr('data-years');