> 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/getattr.md).

# getAttr

### 说明

&#x20;获取`DOM`的特定属性值

### 源码

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

### 用法

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

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

describe('getAttr', () => {
  test('getAttr should return null when nothing matched ', () => {
    document.body.innerHTML += `
      <input type="text" id="text" class="text" />
    `;

    expect(
      getAttr((document.getElementById('text') as HTMLElement), 'disabled')
    ).toBeNull();
  });

  test('getAttr should return the value when matched', () => {
    document.body.innerHTML = `
      <input type="text" id="text" class="text" />
    `;

    expect(getAttr(
      (document.getElementById('text') as HTMLElement),
      'type',
    )).toBe('text');
  });
});
```

{% endcode %}
