{"id":20129348,"url":"https://github.com/ryanmorr/attr","last_synced_at":"2025-04-09T16:09:52.044Z","repository":{"id":41728227,"uuid":"236110429","full_name":"ryanmorr/attr","owner":"ryanmorr","description":"The ultimate DOM attribute, property, style, data, and event setter","archived":false,"fork":false,"pushed_at":"2025-02-19T14:00:30.000Z","size":267,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T16:09:40.665Z","etag":null,"topics":["attribute","dom","javascript","property","style"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanmorr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-25T01:16:42.000Z","updated_at":"2024-11-17T17:30:19.000Z","dependencies_parsed_at":"2024-10-23T23:09:35.160Z","dependency_job_id":"d10902aa-a8a1-40b8-bd5f-f5bb6e98a21b","html_url":"https://github.com/ryanmorr/attr","commit_stats":{"total_commits":42,"total_committers":2,"mean_commits":21.0,"dds":0.2142857142857143,"last_synced_commit":"3a7297b23c5287a6f477f9cb04b53446f74352a9"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Fattr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Fattr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Fattr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanmorr%2Fattr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanmorr","download_url":"https://codeload.github.com/ryanmorr/attr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065283,"owners_count":21041871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attribute","dom","javascript","property","style"],"created_at":"2024-11-13T20:33:42.924Z","updated_at":"2025-04-09T16:09:52.012Z","avatar_url":"https://github.com/ryanmorr.png","language":"JavaScript","readme":"# attr\n\n[![Version Badge][version-image]][project-url]\n[![License][license-image]][license-url]\n[![Build Status][build-image]][build-url]\n\n\u003e The ultimate DOM attribute, property, style, data, and event setter\n\n## Install\n\nDownload the [CJS](https://github.com/ryanmorr/attr/raw/master/dist/cjs/attr.js), [ESM](https://github.com/ryanmorr/attr/raw/master/dist/esm/attr.js), [UMD](https://github.com/ryanmorr/attr/raw/master/dist/umd/attr.js) versions or install via NPM:\n\n``` sh\nnpm install @ryanmorr/attr\n```\n\n## Usage\n\nImport the library:\n\n``` javascript\nimport attr from '@ryanmorr/attr';\n```\n\nAdd an attribute:\n\n``` javascript\nattr(element, 'id', 'foo');\n```\n\nAdd multiple attributes:\n\n``` javascript\nattr(element, {\n    id: 'foo',\n    class: 'bar baz qux'\n});\n```\n\nRemove an attribute with null or undefined:\n\n``` javascript\nattr(element, 'class', null);\n```\n\nSet boolean attributes and properties:\n\n``` javascript\nattr(checkbox, 'checked', true);\nattr(textfield, 'value', 'foo bar');\nattr(element, 'innerHTML', '\u003cspan\u003e\u003c/span\u003e');\n```\n\nSet styles as a string:\n\n``` javascript\nattr(element, 'style', 'width: 100px; height: 100px;');\n```\n\nSet styles as an object:\n\n``` javascript\nattr(element, 'style', {\n    width: 100,\n    height: 100\n});\n```\n\nSet CSS custom properties:\n\n``` javascript\nattr(element, 'style', 'color: var(--color)');\n\nattr(element, 'style', '--color: red');\nattr(element, 'style', {'--color': 'blue'});\n```\n\nAdd an event listener:\n\n``` javascript\nattr(element, 'onclick', (e) =\u003e {\n    // Handle click event\n});\n```\n\nSet data attributes (will automatically convert objects to string):\n\n``` javascript\nattr(element, 'data', {\n    str: 'foo',\n    num: 123,\n    bool: true,\n    object: {foo: 'bar'},\n    array: [1, 2, 3]\n});\n```\n\nSupports functions that return the new value (except when adding an event!). The element and the current value of the attribute are provided as the only 2 parameters:\n\n``` javascript\nattr(element, 'class', 'foo bar baz');\nelement.className; //=\u003e \"foo bar baz\"\nattr(element, 'class', (el, value) =\u003e value.split(' ').filter(cls =\u003e cls !== 'bar'));\nelement.className; //=\u003e \"foo baz\"\n\nattr(element, 'style', {width: 100, height: 100});\nelement.style.cssText; //=\u003e \"width: 100px; height: 100px\"\nattr(element, 'style', (el, value) =\u003e Object.assign({}, value, {height: null}));\nelement.style.cssText; //=\u003e \"width: 100px;\"\n\nattr(element, 'data', {foo: [1, 2, 3]});\nelement.dataset.foo; //=\u003e \"[1,2,3]\"\nattr(element, 'data', (el, value) =\u003e ({foo: value.foo.filter(val =\u003e val !== 2)}));\nelement.dataset.foo; //=\u003e \"[1,3]\"\n```\n\n## License\n\nThis project is dedicated to the public domain as described by the [Unlicense](http://unlicense.org/).\n\n[project-url]: https://github.com/ryanmorr/attr\n[version-image]: https://img.shields.io/github/package-json/v/ryanmorr/attr?color=blue\u0026style=flat-square\n[build-url]: https://github.com/ryanmorr/attr/actions\n[build-image]: https://img.shields.io/github/actions/workflow/status/ryanmorr/attr/node.js.yml?style=flat-square\n[license-image]: https://img.shields.io/github/license/ryanmorr/attr?color=blue\u0026style=flat-square\n[license-url]: UNLICENSE","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanmorr%2Fattr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanmorr%2Fattr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanmorr%2Fattr/lists"}