4.0.0 • Published 4 years ago

@wildpeaks/dom-entry-context v4.0.0

Weekly downloads
17
License
MIT
Repository
github
Last release
4 years ago

DOM Entry Context

Github Release

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); // 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

4.0.0

4 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.0

8 years ago

2.0.1

8 years ago

2.0.0

9 years ago

1.0.0

9 years ago