DOM Entry Context
Typescript function to read JSON-encoded data from an HTMLElement attribute.
Install:
npm install @wildpeaks/dom-entry-context
Example: Read a number from the default attribute data-context
index.html:
<div id="mycontainer" data-context="123">
script.ts:
import {Context, getContext} from '@wildpeaks/dom-entry-context';
const context: Context = getContext('mycontainer');
console.log(context.element);
console.log(context.data);
Example: Read an object from a custom attribute
index.html:
<div id="mycontainer" data-myprop='{"hello": {"world": 123}}'>
script.ts:
import {Context, getContext} from '@wildpeaks/dom-entry-context';
const context: Context = getContext('mycontainer', 'data-myprop');
console.log(context.element);
console.log(context.data);
console.log(context.data.hello);
console.log(context.data.hello.world);