AttributeList

AttributeList

Return a new list of Element attributes.

Constructor

new AttributeList(attrs) → {AttributeList}

Source:
Examples
new AttributeList([{ name: 'class', value: 'foo' }, { name: 'id', value: 'bar' }])
new AttributeList({ class: 'foo', id: 'bar' })
Parameters:
Name Type Description
attrs Array | AttributeList | Object

An array or object of attributes.

Returns:
Type
AttributeList

Extends

  • Array

Methods

(static) from(nodes) → {AttributeList}

Source:

Return a new AttributeList from an array or object.

Examples

Return an array of attributes from a regular object.

AttributeList.from({ dataFoo: 'bar' }) // returns AttributeList [{ name: 'data-foo', value: 'bar' }]

Return a normalized array of attributes from an impure array of attributes.

AttributeList.from([{ name: 'data-foo', value: true, foo: 'bar' }]) // returns AttributeList [{ name: 'data-foo', value: 'true' }]
Parameters:
Name Type Description
nodes Array | AttributeList | Object

An array or object of attributes.

Returns:
Type
AttributeList

add(name, valueopt) → {Boolean}

Source:

Add an attribute or attributes to the current AttributeList.

Examples

Add an empty "id" attribute.

attrs.add('id')

Add an "id" attribute with a value of "bar".

attrs.add({ id: 'bar' })
attrs.add([{ name: 'id', value: 'bar' }])
Parameters:
Name Type Attributes Description
name Array | Object | RegExp | String

The attribute to remove.

value String <optional>

The value of the attribute being added.

Returns:
  • Whether the attribute or attributes were added to the current AttributeList.
Type
Boolean

clone(…attrs) → {Element}

Source:

Return a new clone of the current AttributeList while conditionally applying additional attributes.

Examples
attrs.clone()

Clone the current attribute and add an "id" attribute with a value of "bar".

attrs.clone({ name: 'id', value: 'bar' })
Parameters:
Name Type Attributes Description
attrs Array | AttributeList | Object <repeatable>

Additional attributes to be added to the new AttributeList.

Returns:
  • The cloned Element.
Type
Element

contains(name) → {Boolean}

Source:

Return whether an attribute or attributes exists in the current AttributeList.

Examples

Return whether there is an "id" attribute.

attrs.contains('id')
attrs.contains({ id: 'bar' })

Return whether there is an "id" attribute with a value of "bar".

attrs.contains([{ name: 'id': value: 'bar' }])
Parameters:
Name Type Description
name String

The name or attribute object being accessed.

Returns:
  • Whether the attribute exists.
Type
Boolean

get(name) → {Boolean|Null|String}

Source:

If the attribute exists with a value then a String is returned. If the attribute exists with no value then null is returned. If the attribute does not exist then false is returned.

Example

Return the value of "id" or `false`.

// <div>this element has no "id" attribute</div>
attrs.get('id') // returns false
// <div id>this element has an "id" attribute with no value</div>
attrs.get('id') // returns null
// <div id="">this element has an "id" attribute with a value</div>
attrs.get('id') // returns ''
Parameters:
Name Type Description
name RegExp | String

The name of the attribute being accessed.

Returns:
  • The value of the attribute (a string or null) or false (if the attribute does not exist).
Type
Boolean | Null | String

indexOf(name) → {Number}

Source:

Return the position of an attribute by name or attribute object in the current AttributeList.

Examples

Return the index of "id".

attrs.indexOf('id')

Return the index of /d$/.

attrs.indexOf(/d$/i)

Return the index of "foo" with a value of "bar".

attrs.indexOf({ foo: 'bar' })

Return the index of "ariaLabel" or "aria-label" matching /^open/.

attrs.indexOf({ ariaLabel: /^open/ })

Return the index of an attribute whose name matches `/^foo/`.

attrs.indexOf([{ name: /^foo/ })
Parameters:
Name Type Description
name Array | Object | RegExp | String

The attribute to locate.

Returns:
  • The index of the attribute or -1.
Type
Number

remove(name, valueopt) → {Boolean}

Source:

Remove an attribute or attributes from the current AttributeList.

Examples

Remove the "id" attribute.

attrs.remove('id')

Remove the "id" attribute when it has a value of "bar".

attrs.remove('id', 'bar')
attrs.remove({ id: 'bar' })
attrs.remove([{ name: 'id', value: 'bar' }])

Remove the "id" and "class" attributes.

attrs.remove(['id', 'class'])
Parameters:
Name Type Attributes Description
name Array | Object | RegExp | String

The attribute to remove.

value String <optional>

The value of the attribute being removed.

Returns:
  • Whether the attribute or attributes were removed from the AttributeList.
Type
Boolean

toggle(name_or_attrs, value_or_forceopt, forceopt) → {Boolean}

Source:

Toggle an attribute or attributes from the current AttributeList.

Examples

Toggle the "id" attribute.

attrs.toggle('id')

Toggle the "id" attribute with a value of "bar".

attrs.toggle('id', 'bar')
attrs.toggle({ id: 'bar' })
attrs.toggle([{ name: 'id', value: 'bar' }])
Parameters:
Name Type Attributes Description
name_or_attrs String | Object

The name of the attribute being toggled, or an object of attributes being toggled.

value_or_force String | Boolean <optional>

The value of the attribute being toggled when the first argument is not an object, or attributes should be exclusively added (true) or removed (false).

force Boolean <optional>

Whether attributes should be exclusively added (true) or removed (false).

Returns:
Type
Boolean

toJSON() → {Object}

Source:

Return the current AttributeList as an Object.

Example
attrs.toJSON() // returns { class: 'foo', dataFoo: 'bar' } when <x class="foo" data-foo: "bar" />
Returns:

point - An object version of the current AttributeList

Type
Object

toString() → {String}

Source:

Return the current AttributeList as a String.

Example
attrs.toString() // returns 'class="foo" data-foo="bar"'
Returns:

A string version of the current AttributeList

Type
String