identify-element

2.2.2 • Public • Published

Identify element

A simple way to uniquely identify an HTML element.

Documentation

identifyElement

Returns unique identifier for the provided element.

Parameters

  • element HTMLElement
  • namespace [string] Additional identification of namespace, if you need to identify the same element within several contexts.

Examples

Simple identification of element.

var elm = document.createElement('div');
identifyElement(elm);  // returns e.g. 1
identifyElement(elm);  // every consecutive call returns the same ID, e.g. 1

Identifying element using different namespaces.

var elm = document.createElement('div');
identifyElement(elm, 'aaa');  // returns e.g. 1
identifyElement(elm, 'bbb');  // returns e.g. 2

Returns ?number Number if identifiable element is provided, otherwise null.

isElementIdentified

Checks whether provided element is already identified.

Parameters

Examples

var elm = document.createElement('div');
isElementIdentified(elm); // returns false
identifyElement(elm);
isElementIdentified(elm); // returns true

Returns boolean

getIdentifiedElement

Gets element that was previously identified using identifyElement().

Parameters

Examples

var elm = document.createElement('div');
var id = identifyElement(elm);
getidentifiedElement(id);  // returns elm

Returns ?HTMLElement Identified element if found, otherwise null.

How it works

The function uses data-* attributes to store IDs associated with the element. These attributes have no impact on element's appearance or functionality.

The function should work fine in any browser that supports Element.getAttribute and Element.setAttribute. Which means it works in any modern browser and in IE8+. I didn't bother testing it in older versions of IE.

The generated IDs should be sequential. But don't rely on it, especially if you are using namespaces.

Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at riki@fczbkk.com.

License

Identify element is published under the MIT license.

Readme

Keywords

none

Package Sidebar

Install

npm i identify-element

Weekly Downloads

6

Version

2.2.2

License

MIT

Last publish

Collaborators

  • fczbkk