You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.0 KiB

# bianco.attr
[![Build Status][travis-image]][travis-url]
[![NPM version][npm-version-image]][npm-url]
[![NPM downloads][npm-downloads-image]][npm-url]
[![MIT License][license-image]][license-url]
## Usage
```js
import { set, get, has, remove } from 'bianco.attr'
// set an attribute on a node
const img = document.createElement('img')
set(img, 'width', 100)
get(img, 'width') // => '100'
has(img, 'width') // => true
remove(img, 'width')
get(img, 'width') // => null
```
[travis-image]: https://img.shields.io/travis/biancojs/attr.svg?style=flat-square
[travis-url]: https://travis-ci.org/biancojs/attr
[license-image]: http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
[license-url]: LICENSE.txt
[npm-version-image]: http://img.shields.io/npm/v/bianco.attr.svg?style=flat-square
[npm-downloads-image]: http://img.shields.io/npm/dm/bianco.attr.svg?style=flat-square
[npm-url]: https://npmjs.org/package/bianco.attr
## API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### Table of Contents
- [set](#set)
- [Parameters](#parameters)
- [Examples](#examples)
- [get](#get)
- [Parameters](#parameters-1)
- [Examples](#examples-1)
- [remove](#remove)
- [Parameters](#parameters-2)
- [Examples](#examples-2)
- [has](#has)
- [Parameters](#parameters-3)
- [Examples](#examples-3)
### set
Set any attribute on a single or a list of DOM nodes
#### Parameters
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))** either the name of the attribute to set
or a list of properties as object key - value
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the new value of the attribute (optional)
#### Examples
```javascript
import { set } from 'bianco.attr'
const img = document.createElement('img')
set(img, 'width', 100)
// or also
set(img, {
width: 300,
height: 300
})
```
Returns **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** the original array of elements passed to this function
### get
Get any attribute from a single or a list of DOM nodes
#### Parameters
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to get
#### Examples
```javascript
import { get } from 'bianco.attr'
const img = document.createElement('img')
get(img, 'width') // => '200'
// or also
get(img, ['width', 'height']) // => ['200', '300']
// or also
get([img1, img2], ['width', 'height']) // => [['200', '300'], ['500', '200']]
```
Returns **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** list of the attributes found
### remove
Remove any attribute from a single or a list of DOM nodes
#### Parameters
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to remove
#### Examples
```javascript
import { remove } from 'bianco.attr'
remove(img, 'width') // remove the width attribute
// or also
remove(img, ['width', 'height']) // remove the width and the height attribute
// or also
remove([img1, img2], ['width', 'height']) // remove the width and the height attribute from both images
```
Returns **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** the original array of elements passed to this function
### has
Set any attribute on a single or a list of DOM nodes
#### Parameters
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to detect
#### Examples
```javascript
import { has } from 'bianco.attr'
has(img, 'width') // false
// or also
has(img, ['width', 'height']) // => [false, false]
// or also
has([img1, img2], ['width', 'height']) // => [[false, false], [false, false]]
```
Returns **([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** true or false or an array of boolean values