> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/browser/dom/type/element/+ext/.attr.md).

# elem.attr()

[🔰 JS](/web/js.md) ⟩ [DOM](/web/browser/dom.md) ⟩ [Types](/web/browser/dom/type.md) ⟩ [Element](/web/browser/dom/type/element.md) ⟩ .attr()

{% tabs %}
{% tab title="💾 程式" %}

```javascript
// ⭐️ get/set attribute
Element.prototype.attr = function(name, value){
    if (value !== undefined) { this.setAttribute(name, value) };
    return this.getAttribute(name) || undefined;
};
```

{% endtab %}

{% tab title="⬇️ 應用" %}

* HTML element [attribute](/web/browser/dom/type/element/attribute.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [$(), $all()](/web/browser/dom/querying-elements/dom.md)
* ["data-xxx" Attributes](/web/browser/dom/type/element/attribute/dataset.md)
  {% endtab %}

{% tab title="📘 手冊" %}

* MDN ⟩ Element ⟩ .[setAttribute](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute)()
  {% endtab %}
  {% endtabs %}
