Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 2.06 KB

helpers.md

File metadata and controls

55 lines (41 loc) · 2.06 KB

h

h.getAttrs(prefix, [node]) ⇒ function | object

Get all prefixed data- attributes as an object

Kind: static method of h
Returns: function | object - - curried function for parsing node with passed prefix or parsed attrs

Param Type Description
prefix string data-attribute prefix
[node] HTMLNode target node

Example

  // <div id="n" data-x-a="lol" data-x-b="1" data-y-c='{"key": 1}' data-y-composed-attr="true"></div>
  
  Baz.h.getAttrs('x', window.n) // => {a: "lol", b: 1}
  Baz.h.getAttrs('y', window.n) // => {y: {key: 1}, composedAttr: true}

  const xAttrs = Baz.h.getAttrs('x')
  xAttrs(window.n) // => {x: "lol", b: 1}

h.getChildrenWithData(parentNode, dataKey, [dataValue]) ⇒ NodeList

Query children with specific data-attribute

Kind: static method of h

Param Type Description
parentNode HTMLNode
dataKey string – data-key. data-baz-key, baz-key and bazKey are equivalent
[dataValue] string value of a data-attribute

Example

  // <div id="parent">
  //   <div data-user-id="1">yep</div>
  //   <div data-user-id="2">nope</div>
  // </div>
  
  Baz.h.getChildrenWithData(window.parent, 'data-user-id', 1)[0].textContent === 'yep'
  Baz.h.getChildrenWithData(window.parent, 'user-id', 1)[0].textContent === 'yep'
  Baz.h.getChildrenWithData(window.parent, 'userId', 2)[0].textContent === 'nope'