I want to store an object as an attribute of an HTML element! Let's suppose I'm having a div element and when I click the div element I want to get the values of the object from the div element so that I can use it in the click function.
I've seen people doing it in jquery but I want it in pure javascript since I'm writing a typescript code and I don't want jQuery
ex:
var myNewObject={
"first_name": "Sam",
"last_name": "carter"
}
var div=document.getElementByID("myID");
div.setAttribute("myobject",myNewObject);
<div onclick="somefunc()>
</div>
function somefunc()
{
console.log(document.getAttribute("myobject").first_name);
}
Expected output:
Sam
Actual output:
Error
document.getElementById('id_here').foobar = {"bar":"baz"};localStorage or your own object to keep track of data might suit your needs better though.