Attr: thuộc tính value

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Thuộc tính value của giao diện Attr chứa giá trị của thuộc tính.

Giá trị

Một chuỗi biểu diễn giá trị của thuộc tính.

Ví dụ

Ví dụ sau hiển thị giá trị hiện tại của thuộc tính test. Khi nhấn vào nút, giá trị sẽ được đổi sang một giá trị khác và đọc lại để cập nhật giá trị hiển thị.

HTML

html
<label test="initial value"></label>

<button>Click me to set test to <code>"a new value"</code>…</button>

<p>
  Current value of the <code>test</code> attribute:
  <output id="result">None.</output>
</p>

JavaScript

js
const element = document.querySelector("label");
const button = document.querySelector("button");
const result = document.querySelector("#result");

const attribute = element.attributes[0];
result.value = attribute.value;

button.addEventListener("click", () => {
  attribute.value = "a new value";
  result.value = attribute.value;
});

Thông số kỹ thuật

Specification
DOM
# dom-attr-value

Tương thích trình duyệt