Constructor
new AttributeList(attrs)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
attrs |
AttributeArgument | void | attributes. |
Extends
- Array
Methods
(static) from(attrs)
- Source:
Return a new AttributeList from an array or object of attributes.
Examples
AttributeList.from({ dataFoo: 'bar' }) // returns AttributeList [{ name: 'data-foo', value: 'bar' }]
AttributeList.from([{ name: 'data-foo', value: true, foo: 'bar' }]) // returns AttributeList [{ name: 'data-foo', value: 'true' }]
Parameters:
Name | Type | Description |
---|---|---|
attrs |
Array.<(AttributePartial|AttributePlain)> | Array or object of attributes. |
add(name, …valueopt)
- Source:
Add an attribute or attributes to the current AttributeList, returning whether the attribute or attributes were added.
Examples
attrs.add('id')
attrs.add({ id: 'bar' })
attrs.add([{ name: 'id', value: 'bar' }])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to remove. |
|
value |
Array.<string> |
<optional> <repeatable> |
Value of the attribute to remove. |
clone(…attrs)
- Source:
Return a new clone of the current AttributeList while conditionally applying additional attributes.
Examples
attrs.clone()
attrs.clone({ name: 'id', value: 'bar' })
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
attrs |
Array.<(AttributeArgument|RegExp|string)> |
<repeatable> |
Additional attributes to be added to the new AttributeList. |
contains(name)
- Source:
Return whether an attribute or attributes exists in the current AttributeList.
Examples
attrs.contains('id')
attrs.contains({ id: 'bar' })
attrs.contains([{ name: 'id': value: 'bar' }])
Parameters:
Name | Type | Description |
---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to check. |
get(name) → {false|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
// <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 |
AttributeArgument | RegExp | string | Name or attribute object being accessed. |
Returns:
- Type
- false | null | string
indexOf(name, …valueopt)
- Source:
Return the position of an attribute by name or attribute object in the current AttributeList.
Examples
attrs.indexOf('id')
attrs.indexOf(/d$/i)
attrs.indexOf({ foo: 'bar' })
attrs.indexOf({ ariaLabel: /^open/ })
attrs.indexOf([{ name: /^foo/ })
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to locate. |
|
value |
Array.<(RegExp|string)> |
<optional> <repeatable> |
Value of the attribute to locate. |
remove(name, …valueopt)
- Source:
Remove an attribute or attributes from the current AttributeList, returning whether any attribute was removed.
Examples
attrs.remove('id')
attrs.remove('id', 'bar')
attrs.remove({ id: 'bar' })
attrs.remove([{ name: 'id', value: 'bar' }])
attrs.remove(['id', 'class'])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
AttributeArgument | RegExp | string | Attribute to remove. |
|
value |
Array.<string> |
<optional> <repeatable> |
Value of the attribute being removed. |
toggle(attr, …valueopt, forceopt)
- Source:
Toggle an attribute or attributes from the current AttributeList, returning whether any attribute was added.
Examples
attrs.toggle('id')
attrs.toggle('id', 'bar')
attrs.toggle({ id: 'bar' })
attrs.toggle([{ name: 'id', value: 'bar' }])
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
attr |
AttributeArgument | RegExp | string | Attribute to toggle. |
|
value |
Array.<{0: boolean}> | Array.<{0: string}> | Array.<{0: string, 1: boolean}> |
<optional> <repeatable> |
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). |
toJSON() → {AttributePlain}
- Source:
Return the current AttributeList as a stringifiable Object.
Example
attrs.toJSON() // returns { class: 'foo', dataFoo: 'bar' } when <x class="foo" data-foo: "bar" />
Returns:
- Type
- AttributePlain
toString()
- Source:
Return the current AttributeList as a String.
Example
attrs.toString() // returns 'class="foo" data-foo="bar"'