HTMLInputElement: labels property

HTMLInputElement.labels 읽기 전용 속성은 요소가 숨겨져 있지 않은 경우 <input> 요소와 연관된 <label> 요소의 NodeList를 반환합니다. 요소의 타입이 hidden인 경우, 속성은 null을 반환합니다.

<input> 요소와 연관된 <label> 요소를 포함하는 NodeList입니다.

예제

HTML

html
<label id="label1" for="test">Label 1</label>
<input id="test" />
<label id="label2" for="test">Label 2</label>

JavaScript

js
window.addEventListener("DOMContentLoaded", () => {
  const input = document.getElementById("test");
  for (const label of input.labels) {
    console.log(label.textContent); // "Label 1" 과 "Label 2"
  }
});

명세서

Specification
HTML Standard
# dom-lfe-labels-dev

브라우저 호환성

BCD tables only load in the browser