> For the complete documentation index, see [llms.txt](https://ddzy.gitbook.io/ts-utility-plugins-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ddzy.gitbook.io/ts-utility-plugins-docs/utility/utility-dom/setattr.md).

# setAttr

### 说明

&#x20;设置单个`DOM`属性

### 源码

<https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/dom/setAttr>

### 用法

{% code title="setAttr.test.ts" %}

```typescript
import { setAttr } from "../../utility/dom/setAttr";

describe('setAttr', () => {
  test('setAttr should receive a DOM object and attributes, add attributes to DOM, and return nothing', () => {
    document.body.innerHTML = `
      <input id="text"></div>
    `;

    expect(setAttr(
      document.getElementById('text') as HTMLElement,
      {
        class: 'text',
        type: 'text',
      }
    ));
  });
});
```

{% endcode %}
