Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/callumacrae/obj-to-attrs
:arrow_right: Turns a JavaScript object into a string containing HTML attributes.
https://github.com/callumacrae/obj-to-attrs
Last synced: 3 months ago
JSON representation
:arrow_right: Turns a JavaScript object into a string containing HTML attributes.
- Host: GitHub
- URL: https://github.com/callumacrae/obj-to-attrs
- Owner: callumacrae
- Created: 2015-01-13T14:49:36.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-01-13T15:10:20.000Z (about 10 years ago)
- Last Synced: 2024-10-11T00:10:39.706Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 133 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# obj-to-attrs
Turns a JavaScript object into a string containing HTML attributes.
## Install
```
$ npm install --save obj-to-attrs
```## Usage
```js
var objToAttrs = require('obj-to-attrs');objToAttrs({
width: 100,
height: 100,
style: 'font-color: red',
dataFoo: 'bar',
checked: true
});
//=> width="100" height="100" style="font-color: red" data-foo="bar" checked
```You can also specify options as a second argument, letting you change the
assignment operator, the quotes used, and and separator between attributes.```js
var obj = { width: 100, height: 100 };
objToAttrs(obj, {
assignment: ' = ',
quote: "'",
separator: ' '
});
// => width = '100' height = '100'
```Finally, it also includes a data attribute helper like in Rails, so you can use
an object to make lots of data attributes.```js
objToAttrs({
value: 'test',
data: {
foo: 'bar',
hello: 'world'
}
});
// => value="test" data-foo="bar" data-hello="world"
```You can also add your own helpers. It's pretty simple; read the code.
## License
Released under the MIT license.