README
DOM Entry Context
Typescript function to read JSON-encoded data from an HTMLElement attribute.
Install:
npm install @wildpeaks/dom-entry-context
data-context
Example: Read a number from the default attribute 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); // Reference to #mycontainer
console.log(context.data); // 123
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); // Reference to #mycontainer
console.log(context.data); // {"hello": {"world": 123}}
console.log(context.data.hello); // {"world": 123}
console.log(context.data.hello.world); // 123