{"id":13548032,"url":"https://github.com/spatie/spatie-dom","last_synced_at":"2025-04-22T20:30:45.877Z","repository":{"id":57367032,"uuid":"73085248","full_name":"spatie/spatie-dom","owner":"spatie","description":"A small DOM querying and manipulation library","archived":false,"fork":false,"pushed_at":"2022-03-21T12:45:19.000Z","size":47,"stargazers_count":39,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-28T22:43:52.250Z","etag":null,"topics":["dom","javascript","manipulation"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-07T14:16:34.000Z","updated_at":"2024-04-02T20:17:12.000Z","dependencies_parsed_at":"2022-08-23T20:10:48.145Z","dependency_job_id":null,"html_url":"https://github.com/spatie/spatie-dom","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fspatie-dom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fspatie-dom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fspatie-dom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fspatie-dom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/spatie-dom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222871078,"owners_count":17050600,"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":["dom","javascript","manipulation"],"created_at":"2024-08-01T12:01:04.802Z","updated_at":"2024-11-03T16:32:02.673Z","avatar_url":"https://github.com/spatie.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1\" /\u003e](https://supportukrainenow.org)\n\n# spatie-dom\n\n[![Latest Version on NPM](https://img.shields.io/npm/v/spatie-dom.svg?style=flat-square)](https://npmjs.com/package/spatie-dom)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Build Status](https://img.shields.io/travis/spatie/spatie-dom/master.svg?style=flat-square)](https://travis-ci.org/spatie/spatie-dom)\n\nA small DOM querying and manipulation library.\n\nSpatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/spatie-dom.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/spatie-dom)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Install\n\nYou can install the package via yarn:\n\n```bash\nyarn add spatie-dom\n```\n\n## Usage\n\n### Querying the DOM\n\nThe DOM can be queried with `query` and `queryAll`, which are wrappers around [`querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) and [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll).\n\nQuerying an element in `document`:\n\n```html\n\u003cdiv id=\"app\"\u003e\u003c/div\u003e\n```\n\n```js\nconst app = query('#app'); // Returns a `HTMLElement`\n```\n\nQuerying a collection of elements:\n\n\u003e `queryAll` returns a plain array instead of the usual `NodeList` collection\n\n```html\n\u003cdiv id=\"main\"\u003e\n    \u003carticle\u003e\u003c/article\u003e\n    \u003carticle\u003e\u003c/article\u003e\n\u003c/div\u003e\n```\n\n```js\nconst articles = queryAll('#main \u003e article'); // Returns an array `Array\u003cHTMLElement\u003e`\n```\n\n`query` and `queryAll` also accept a scope as their second argument (by default, the scope is `document`).\n\n```html\n\u003cdiv id=\"main\"\u003e\n    \u003ch1\u003eHeader\u003c/h1\u003e\n\u003c/div\u003e\n```\n\n```js\nconst main = query('#main'); // Returns a `HTMLElement`\nconst header = query('h1', main); // Also returns a `HTMLElement`\n```\n\n### Retrieving 'props'\n\nProps are DOM attributes that exist to be consumed by scripts. Props behave just like attributes, except they get parsed as JSON if prefixed by a `:`.\n\n\u003e This syntax is heavily based on what Vue uses for component props\n\n```html\n\u003cdiv\n    id=\"component\"\n    my-prop=\"foo\"\n    :config='{ \"url\": \"bar\" }'\n\u003e\u003c/div\u003e\n```\n\n```js\nimport { query, prop, props } from  'spatie-dom';\n\nconst el = query('#component');\n\nprop(el, 'myProp'); // 'foo'\nprop(el, 'config'); // { url: 'bar' }\n\nprops(el); // { myProp: 'foo', config: { url: 'bar' }}\n```\n\n### Firing events based on the DOM state\n\nThe `whenReady` function calls a function:\n- immediately if the DOM is loaded;\n- otherwise after the `document` `DOMContentLoaded` event\n\n```js\nimport { whenReady } from  'spatie-dom';\n\nwhenReady(() =\u003e console.log('Ready!'));\n```\n\nThe `whenLoaded` function calls a function:\n- immediately if the DOM and all subresources (scripts, images,...) are loaded;\n- otherwise after the `window` `load` event\n\n```js\nimport { whenLoaded } from  'spatie-dom';\n\nwhenLoaded(() =\u003e console.log('Loaded!'));\n```\n\n### Reading the DOM\n\nThere are several functions to read data from the dom.\n\nWith `attribute`, you can retrieve an attribute, and with `data`, you can retrieve a data attribute.\n\n```html\n\u003cdiv id=\"element\" data-foo=\"bar\"\u003e\u003c/div\u003e\n```\n\n```js\nimport { attribute, data, query };\n\nconst el = query('#element');\n\n// Retrieve an attribute\nattribute('id', el); // 'element'\n\n// Retrieve an attribute with a fallback value\nattribute('class', el, 'active'); // 'active'\n\n// Retrieve a data attribute\ndata('foo', el); // 'bar'\n\n// Retrieve a data attribute with a fallback value\ndata('baz', el, 'qux'); // 'qux'\n```\n\n## Full API\n\n### Attribute\n\n```ts\nfunction attribute(name: string, el: HTMLElement, fallback: string = ''): string\n```\n\n### Data\n\n```ts\nfunction data(name: string, el: HTMLElement, fallback: string = ''): string\n```\n\n### On\n\n```ts\nfunction on(event: string, subject: HTMLElement, handler: Function): string\n```\n\n### Props\n\n```ts\nfunction prop(el: HTMLElement, name: string, fallback: any = null): any;\n\nfunction props(el: HTMLElement): Object;\n```\n\n### Query\n\n```ts\nfunction query(selector: string): HTMLElement | null;\nfunction query(selector: string, scope: HTMLElement | Document): HTMLElement | null;\n\nfunction queryAll(selector: string): Array\u003cHTMLElement\u003e;\nfunction queryAll(selector: string, scope: HTMLElement | Document): Array\u003cHTMLElement\u003e;\n```\n\n### When\n\n```ts\nfunction whenReady(callback: Function): void\n\nfunction whenLoaded(callback: Function): void\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ npm run test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you discover any security related issues, please contact [Sebastian De Deyne](https://github.com/sebastiandedeyne) instead of using the issue tracker.\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## About Spatie\n\nSpatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fspatie-dom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fspatie-dom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fspatie-dom/lists"}